关于vue组件销毁之后计时器继续执行的解决办法

直接上代码,以下
window.setTimeout(function(){
    if(this && !this._isDestroyed){ //_isDestroyed 组件是否被销毁
        return;
    }
    window.location.href = "/serverMonitor?t="+ new Date().getTime();
},5000)

很粗暴的方法,在执行之前看是否被销毁就行,

第二种方法,调用路由组件内的钩子函数beforeRouteLeave

beforeRouteLeave(to,from,next){
clearTimeout(window.timer);
next();
}


猜你喜欢

转载自blog.csdn.net/weixin_41557291/article/details/80569844