'How to set the (correct!) savedPosition in Vue-router?

For reasons I can't figure out the Y-value of the savedPosition never goes over 2000, even though window.pageYOffset reports me the correct Y-value if I ask it just before I push a new route.

console.log(window.pageYOffset) // tells me: 4233
this.$router.push({params:{id:newId}})

Hit backbutton.. router:

export default new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  scrollBehavior: (to, from, savedPosition) => {
    console.log('scrollBehavior',savedPosition) // tells me: 1557
  }
  routes: [ ... ]
}

The savedPositon seems to fluctuate beteen 1500 and 2000px, regardless how deep I am in the page. The window.pageYOffset always gives me the correct value.

I've tried returning a promise to wait a few seconds before scrolling, but that doesn't matter. If anyone has any idea's why this is or how to fix it, great, otherwise, somehow getting this window.pageYOffset value in the scrollBehavior would also be acceptable.

Also this concerns vue-router 3.x.. I'm migrating to vue-router 4.x in a few months time. So ideally it works in both or in 4.x only.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source