vue2中如何在组件销毁时销毁定时器以及移除监听

beforeDestroy(){
    
     
 // 离开当前路由前的操作
  clearInterval(timer)
  timer = null
}

destroyed(){
    
    
  // 离开当前路由后的操作
  clearInterval(timer)
  timer = null
}
//建议用这个
this.$once('hook:beforeDestroy', () => {
    
    
  // $once一次性监听事件,触发后即关闭
  // hook 自定义钩子函数

  clearInterval(timer)
  timer = null
})

猜你喜欢

转载自blog.csdn.net/weixin_44162077/article/details/130101798