业务中有时候需要定时器来轮询接口,关闭页面时需要销毁定时器,要不然会导致内存泄漏

export default {
  setup () {
    const state = reactive({
      timeInter:null,//定义定时器
    })
    onMounted(()=>{
      state.timeInter = setInterval(()=>{
        //业务代码
      },3000)
    })
    onUnmounted (()=>{
      clearInterval(state.timeInter) //销毁
      state.timeInter = null
    })
    return {
        ...toRefs(state)
    };
  },
}
Logo

前往低代码交流专区

更多推荐