Vue2.5 旅游项目实例21 详情页 对全局事件的解绑

上次在Header.vue中写了一个:

activated () {
    window.addEventListener('scroll', this.handleScroll)
  },

这个是对全局的 window 对象,所以会出现问题,不仅对这个组件,也对全局的组件都造成了影响。

解决办法:

// 页面即将被隐藏 或者说 页面即将被替换成新的页面时,deactivated会被执行
  deactivated () {
    window.removeEventListener('scroll', this.handleScroll)
  },

也就是页面展示的时候去绑定 scroll 事件,而页面被隐藏的时候,再去对 scroll 这个全局事件进行一个解绑。

然后再提交下代码:

git add .
git commit -m "对全局事件解绑"
git push

git checkout detail-header
git merge master
git push
原文地址:https://www.cnblogs.com/joe235/p/12511158.html