mirror of
https://github.com/youzan/vant.git
synced 2026-06-13 01:07:14 +08:00
[Doc] support title anchor (#3845)
This commit is contained in:
@@ -20,11 +20,17 @@ importAll(docs, docsFromPackages);
|
||||
|
||||
const router = new VueRouter({
|
||||
mode: 'hash',
|
||||
routes: routes({ componentMap: docs })
|
||||
routes: routes({ componentMap: docs }),
|
||||
scrollBehavior(to, from, savedPosition) {
|
||||
if (to.hash) {
|
||||
return { selector: to.hash };
|
||||
}
|
||||
|
||||
return savedPosition || { x: 0, y: 0 };
|
||||
}
|
||||
});
|
||||
|
||||
router.afterEach(() => {
|
||||
window.scrollTo(0, 0);
|
||||
router.afterEach(path => {
|
||||
Vue.nextTick(() => window.syncPath());
|
||||
});
|
||||
|
||||
@@ -36,6 +42,18 @@ if (process.env.NODE_ENV !== 'production') {
|
||||
|
||||
new Vue({
|
||||
el: '#app',
|
||||
mounted() {
|
||||
if (this.$route.hash) {
|
||||
setTimeout(() => {
|
||||
const el = document.querySelector(this.$route.hash);
|
||||
if (el) {
|
||||
el.scrollIntoView({
|
||||
behavior: 'smooth'
|
||||
});
|
||||
}
|
||||
}, 500);
|
||||
}
|
||||
},
|
||||
render: h => h(App),
|
||||
router
|
||||
});
|
||||
|
||||
@@ -39,6 +39,7 @@ const registerRoute = ({ mobile, componentMap }) => {
|
||||
component = DemoPages;
|
||||
} else if (mobile) {
|
||||
const module = componentMap[`./${path}/demo/index.vue`];
|
||||
|
||||
if (module) {
|
||||
component = demoWrapper(module, path);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,10 @@ export function initIframeRouter() {
|
||||
|
||||
window.changePath = function (lang, path = '') {
|
||||
setLang(lang);
|
||||
window.vueRouter.replace(path);
|
||||
|
||||
// should preserve hash for anchor
|
||||
if (window.vueRouter.currentRoute.path !== path) {
|
||||
window.vueRouter.replace(path);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user