vue 生命周期 beforeDestroy 调用
1. 离开当前路由,会直接调用
当前路由不使用 缓存
,离开当前路由会直接调用 beforeDestroy 和 beforeDestroy 销毁
2.离开当前路由,不会直接调用,需要程序控制调用;
当前路由使用 缓存
,离开当前路由不会直接调用 beforeDestroy 和 beforeDestroy 销毁,需要使用路由钩子函数
主动的调用
beforeRouteLeave(to, from, next) {
this.$destroy();
next();
}
3.应用于全局事件
如果把事件绑定到window上面比如scroll事件,那么在推出这个页面的时候一定要进行解绑
,不然在其他的页面也会受到这个事件的影响,造成bug
mounted () {
window.addEventListener('scroll', this.handleScroll)
},
beforeDestroy () {
window.removeEventListener('scroll', this.handleScroll)
}