How can I prevent the page from scrolling to the top when I change the route's query param (and the view's props)?
I've tried the following with no luck:
Attempt 1 - Route's component
When I make the timeout an arbitrarily large number (1 second) then it scrolls back down after some delay.
// in my route's component
props: {...},
watch: {
$route(to, from) {
let y = window.scrollY;
this.$nextTick(() => {
setTimeout(() => {
console.log(`scrolling to ${y}`);
window.scrollTo(0, y);
}, 0);
});
}
}
Attempt 2 - $router's scrollBehavior
This logs the correct y value, but doesn't maintain the old position.
scrollBehavior(to, from, savedPosition) {
if (savedPosition) {
return savedPosition;
}
if (from.path !== to.path) {
return { x: 0, y: 0 };
}
let existing = {
x: window.scrollX,
y: window.scrollY
};
console.log(`Existing scroll`, existing);
return new Promise(resolve => {
setTimeout(() => {
resolve(existing);
}, 0);
});
},