Article From:https://www.cnblogs.com/sunjuncoder/p/9971922.html
beforeRouteEnter (to, from, next) {
    let aScrollY = store.getters.aScrollY
    setTimeout(function () {
      window.scrollTo(0, aScrollY);
    }, 0) //Synchronous to asynchronous operation*/
    /* The page is kept alive, so you can use it directly when setTimeout 0 is set. If your page is not kept alive, you need to wait until next Tick or setTimeout is set long enough before scrollT is executed.O.*/
    next()

  },
  beforeRouteLeave (to, from, next) {//Record the location of departure
    let position = window.scrollY
    this.$store.commit('changeaScrollY', position)
    next()

  },
import store from '../store'

If area scrolling is used

import Bscroll from 'better-scroll'
beforeRouteEnter (to, from, next) {
    next(vm => {
      console.log(vm)  //vmExample for Vue
      vm.$nextTick(() => {
        vm.scroll = new Bscroll(vm.$refs.qiehuan)
        let bScrollY = store.getters.bScrollY
        vm.scroll.scrollTo(0, bScrollY)
        console.log(vm.scroll.y)//yNegative
      })
    })

  },
  beforeRouteLeave (to, from, next) {//Record the location of departure
    console.log(this.scroll.y)

    let position = this.scroll.y
    this.$store.commit('changebScrollY', position)
    next()

  }

 next(vm=>{console.log(‘next’) }) 
The code in this is executed very late, after the component mounted cycle

Leave a Reply

Your email address will not be published. Required fields are marked *