Skip to content

Commit a7b3e22

Browse files
committed
✨ add router scroll behaviour
1 parent b3c2f68 commit a7b3e22

File tree

1 file changed

+20
-6
lines changed
  • packages/@vuepress/core/lib/app

1 file changed

+20
-6
lines changed

packages/@vuepress/core/lib/app/app.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if (module.hot) {
2323
if (siteData.base !== prevBase) {
2424
window.alert(
2525
`[vuepress] Site base has changed. ` +
26-
`Please restart dev server to ensure correct asset paths.`
26+
`Please restart dev server to ensure correct asset paths.`
2727
)
2828
}
2929
})
@@ -62,15 +62,25 @@ export function createApp (isServer) {
6262
base: siteData.base,
6363
mode: 'history',
6464
fallback: false,
65-
routes
65+
routes,
66+
scrollBehavior (to, from, savedPosition) {
67+
if (savedPosition) {
68+
return savedPosition
69+
}
70+
if (to.path !== from.path) {
71+
return { x: 0, y: 0 }
72+
}
73+
}
6674
})
6775

6876
// redirect /foo to /foo/
6977
router.beforeEach((to, from, next) => {
7078
if (!/(\/|\.html)$/.test(to.path)) {
71-
next(Object.assign({}, to, {
72-
path: to.path + '/'
73-
}))
79+
next(
80+
Object.assign({}, to, {
81+
path: to.path + '/'
82+
})
83+
)
7484
} else {
7585
next()
7686
}
@@ -94,7 +104,11 @@ export function createApp (isServer) {
94104
render (h) {
95105
return h('div', { attrs: { id: 'app' }}, [
96106
h('router-view', { ref: 'layout' }),
97-
h('div', { class: 'global-ui' }, globalUIComponents.map(component => h(component)))
107+
h(
108+
'div',
109+
{ class: 'global-ui' },
110+
globalUIComponents.map(component => h(component))
111+
)
98112
])
99113
}
100114
})

0 commit comments

Comments
 (0)