The VUE cache page returns to the specified scroll position

Registering rolling events in VUE is no different from DOM

The following is used with the Keep-alive component

  1. Mounted Registers the this.handleScroll event to retrieve the scrollTop

    mounted(){ window.addEventListener('scroll', this.handleScroll); } handleScroll () { let scrollTop = document.body.scrollTop; this.scroll = scrollTop; }Copy the code
  2. Called when the keep-alive component is activated. This hook is not called during server-side rendering

    activated() { if(this.scroll > 0){ window.scrollTo(0, this.scroll); this.scroll = 0; window.addEventListener('scroll', this.handleScroll); }}Copy the code
  3. Called when the keep-alive component is disabled. This hook is not called during server-side rendering.

     deactivated(){
         window.removeEventListener('scroll', this.handleScroll);
     } 
    Copy the code