const router = new VueRouter({
routes: [...],
scrollBehavior (to, from, savedPosition) {
// 第三个参数 savedPosition 当且仅当 popstate 导航 (通过浏览器的前进/后退按钮触发) 时才可用
// return 期望滚动到哪个的位置
}
})
{ x: number, y: number }
// or
{ selector: string, x: number, y: number }
// 在 2.6.0+ 中可以使用 offset
{ selector: string, offset : { x: number, y: number } }
scrollBehavior (to, from, savedPosition) {
return { x: 0, y: 0 }
}
scrollBehavior (to, from, savedPosition) {
if (savedPosition) {
return savedPosition
} else {
return { x: 0, y: 0 }
}
}
scrollBehavior (to, from, savedPosition) {
if (to.hash) {
return {
selector: to.hash
}
}
}
scrollBehavior (to, from, savedPosition) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ selector:to.hash })
}, 1000)
})
}