一、问题描述:
页面跳转时,跳转到新的页面,新的页面的位置,保持了和上一次滚动到的页面位置,没有自动定位到新页面的顶部
二、解决方案:
在 router下的index.js 的路由中配置如下:
1.路由模式hash时
const router = new VueRouter({
// history模式,去除URL中#
mode: 'history',
routes,
// 解决路由跳转后,不显示在首行
scrollBehavior() {
return {x: 0, y: 0};
}
});
2.路由模式为history时
//修改后
const routes = []
...
const scrollBehavior = (to, from, savedPosition) => {
if (savedPosition && to.meta.keepAlive) {
return savedPosition;
}
return { left: 0, top:0 };
}
const router = new VueRouter({
routes,
scrollBehavior,
})