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

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

 

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

 

 

你可能感兴趣的:(vue)