解决:通过vue-router自带的scrollBehavior
配置项进行设置;
创建router实例;
const router = createRouter({
history: createWebHashHistory(),
routes:[...],
scrollBehavior (to, from, savedPosition) {
// (top代替以前y, 设置0, 代表垂直方向滚动条回到顶端)
//路由跳转之后,页面始终滚动到顶部
return { top: 0 };
}
});
具体见vue-router文档:https://router.vuejs.org/zh/guide/advanced/scroll-behavior.html