【vue优化】 事件的销毁
事件的销毁Vue 组件销毁时,会自动解绑它的全部指令及事件监听器,但是仅限于组件本身的事件而对于定时器、addEventListener注册的监听器等,就需要在组件销毁的生命周期钩子中手动销毁或解绑,以避免内存泄露<script>exportdefault{created(){this.timer=setInterval(this.refresh,2000)addEventListen
·
事件的销毁
Vue 组件销毁时,会自动解绑它的全部指令及事件监听器,但是仅限于组件本身的事件
而对于定时器
、addEventListener
注册的监听器等,就需要在组件销毁的生命周期钩子中手动销毁或解绑,以避免内存泄露
<script>
export default {
created() {
this.timer = setInterval(this.refresh, 2000)
addEventListener('touchmove',
this.touchmove, false)
},
beforeDestroy() {
clearInterval(this.timer)
this.timer = null
removeEventListener('touchmove',
this.touchmove, false)
}
}
</script>
更多推荐
已为社区贡献5条内容
所有评论(0)