Vue 定时器清除方案优化
作者:互联网
写在一个函数中方便优化
getAddNum() {
const timer = setInterval(() => {
this.nums = this.nums+1
}, 500);
// 通过$once来监听定时器,在beforeDestroy钩子可以被清除。
this.$once("hook:beforeDestroy", () => {
clearInterval(timer);
});
}
在任意地方用 this.$on/$once('hook:生命周期',callback),就可以监听到生命周期的变化了
标签:定时器,nums,清除,beforeDestroy,timer,hook,Vue,once 来源: https://www.cnblogs.com/1024L/p/15442820.html