当前位置: 代码迷 >> 综合 >> 无unmounted解绑,使用destroyed狗子解绑window.addEventListener
  详细解决方案

无unmounted解绑,使用destroyed狗子解绑window.addEventListener

热度:53   发布时间:2024-01-28 02:26:00.0

在handleScroll()方法中添加console.log(‘scroll’);发现回到首页,其实 window.addEventListener(‘scroll’, this.doScroll)事件并没有被销毁,滚动时,控制台仍能输出‘scroll’,vue官网上也并没有找到unmounted()函数。使用destroyed进行解绑销毁

 

  methods: {handleScroll() {console.log('scroll')const top = document.documentElement.scrollTopif (top > 60) {let opacity = top / 140opacity = opacity > 1 ? 1 : opacitythis.opacityStyle = {opacity}this.showAbs = false}else {this.showAbs = true}}},mounted() {window.addEventListener('scroll', this.handleScroll)},destroyed() {window.removeEventListener('scroll', this.handleScroll)}

 

 

  相关解决方案