利用$once绑定beforeDestroy事件,避免在data上添加timer属性清除定时器。
export default {
mounted() {
this.createInterval()
},
methods:{
createInterval(){
let timer = setInterval(()=>{
console.log(new Date().toLocaleTimeString())
},1000)
this.$once('hook:beforeDestroy',()=>{
clearInterval(timer)
})
}
}
}