上代码:
let arr = {}
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes,
//页面之间无位置影响 设置为暂存的位置
scrollBehavior(to, from, savedPosition) {
let y = arr[to.name]
return {x: 0, y: y}
}
})
//路由前置守卫暂存scroll位置
router.beforeEach((to, from, next) => {
arr[from.name] = document.documentElement.scrollTop
next()
})