Vue · Senior · Question 40 of 75
How do you restore scroll position when the user navigates back?
createRouter({
history: createWebHistory(),
routes,
scrollBehavior: (to, from, saved) => saved ?? { top: 0 },
});- ✓Correct answer: With the
scrollBehavioroption, returning the saved position for back/forward navigation - 2Browsers handle it automatically in SPAs
- 3With
window.scrollToin every component
Explanation
saved is provided only for popstate navigations. You can also scroll to to.hash or return a Promise to wait for async content.