1 回答
TA贡献1818条经验 获得超3个赞
支持二级路由啊,我的代码是这样写的。
router/index.js
const scrollBehavior = (to, from, savedPosition) => {
if (savedPosition) {
// savedPosition is only available for popstate navigations.
return savedPosition;
} else {
const position = {}
// new navigation.
// scroll to anchor by returning the selector
if (to.hash) {
position.selector = to.hash
}
// check if any matched route config has meta that requires scrolling to top
if (to.matched.some(m => m.meta.scrollToTop)) {
// cords will be used if no selector is provided,
// or if the selector didn't match any element.
position.x = 0
position.y = 0
}
// if the returned position is falsy or an empty object,
// will retain current scroll position.
return position;
}
};
let router = new Router({
mode: 'history',
scrollBehavior,
routes: [
{
path: '/detail/:id',
name: 'Detail',
component: Detail,
meta: {
scrollToTop: true
}
}
]
});
添加回答
举报