The initial page of vue2 project page switching is displayed to the top area of the page by default.

question:

Problem requirement: When entering each vue page, the top of the page is displayed by default

The cause of the problem: If you do not set it, the page position displayed on the new page of Vue will default to the position previewed on the previous page, which will be very inconvenient when viewing the product details page

Solution to the problem:

Find the settings in router/index.js, add the following code, and add the location in const router = new VueRouter :

scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }

Case — such as:

Guess you like

Origin blog.csdn.net/qq_63310300/article/details/128950086