When caching a page with keep-alive, another page will execute the method of the previous page?

I click an item from the list to enter the details, and I will re-request the data when I return to the page, so I use keep-alive to cache the page and find that the scroll to the address I wrote on the list page will load more to the details page.
index.js

{
      path: "/",
      name: "index",
      component: index,
      meta: { keepAlive: true }
 },
{
      path: "/newinfo",
      name: "newinfo",
      component: newinfo
},

index.vue 

loadmore(){
let that=this
if (($(window).scrollTop() + $(window).height()) >=$(document).height()-100  &&that.isbool==true) {
                that.isbool=false; 
                setTimeout(() => {
                that.more() 
                that.isbool=true; 
                }, 1000);
            } 
      },  
index.vue 
activated(){
if (screen.width<768) {
window.addEventListener("scroll",this.loadmore)
}
}
   
index.vue
deactivated(){
window.removeEventListener("scroll",this.loadmore)
},   

Aug.03,2021

not only do you keep listening when you keep-alive , the listening events of document are exactly independent of vue items, and will always exist if you don't destroy them.

you can drop events you have previously listened to in the vue hook function: beforeDestroy or destroy hook remove .

destroy () { // beforeDestroy
    document.body.removeEventListener('onscroll', this.scroll) // 
}

if the project uses the keep-alive cache, you can try to call the function that removes the listener when the hook deactivated dynamic component is destroyed. Similarly, there is the hook activated , and the official document stamps this: keep-alive

.
deactivated () {
    document.body.removeEventListener('onscroll', this.scroll) // 
}

I have this problem, too. Have you solved this problem


methods () {
  document.body.addEventListener('onscroll', this.scroll)
},
watch: {
  $route (val) {
    if (val.path === '/') {
      document.body.addEventListener('onscroll', this.scroll)
    } else {
      document.body.removeEventListener('onscroll', this.scroll)
    }
  }
},
methods: {
  scroll () {
    // 
  }
}

have you solved it

?
Menu