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 },
});

Explanation

saved is provided only for popstate navigations. You can also scroll to to.hash or return a Promise to wait for async content.

Further reading

Problem with a question

Vue · Senior · question 40

Test yourself

Take the Vue · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz