vue路由跳转后 removeEventListener 移除滚动事件不起作用怎么办?

Bug:A页面路由跳转B页面后,“滚动条下拉到底部加载更多”事件移除未生效,在B页面滚动条下拉到底部还是一直在请求A页面的接口。
代码如下:


        methods: {
            //点击事件路由跳转B页面
              goCommodityDetails(e){
                  window.removeEventListener('scroll', this.addEventScrollTop ,true)
                  this.$router.push({
                    path: '/commodityDetails',
                    query: {
                        t: Math.random(),
                        id: e.id
                    }
                });
              },
          },
          created () {
            window.addEventListener('scroll', this.addEventScrollTop ,true)
          },
          mounted () {

          },
          updated() {
 
          },
          destroyed(){
              window.removeEventListener('scroll', this.addEventScrollTop ,true)
          }
阅读 8.3k
3 个回答
//window.removeEventListener('scroll', this.addEventScrollTop ,true)
window.removeEventListener('scroll', this.addEventScrollTop ,false)

同样的问题,刚刚解决

使用beforeRouteLeave试试。
beforeRouteLeave(to,from,next){
    window.removeEventListener('scroll', this.addEventScrollTop ,true);
    this.$router.push({
       path: '/commodityDetails',
           query: {
            t: Math.random(),
             id: e.id
              }
         });
    next();// next()记得写不然不跳转
}

没试一试在beforedestroy 这个钩子时remove?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题