我配置了以下路由器:
const router = new Router({
mode: 'history',
routes: [
// root
{
path: '/',
component: ComponentPage,
redirect: routePrefix.public,
children: [
// public
{
path: routePrefix.public,
component: ComponentPage,
redirect: `${routePrefix.public}/login`,
children: [
{
path: `${routePrefix.public}/login`, component: LoginPage,
}],
},
// private
{
path: routePrefix.private,
component: ComponentPage,
children: [
{
path: `${routePrefix.private}/u`, component: PrivatePage,
}],
}],
}],
});
现在你可以看到,有两个主要部分;一个公共的,一个私人的 . 另外,我还为授权配置了以下导航防护装置:
router.beforeEach((to, from, next) => {
console.log(`registered request to redirect from
'${from.fullPath}' to '${to.fullPath}'`);
if (to.fullPath.startsWith(routePrefix.private) &&
!Store.getters['auth/isAuthenticated']) {
console.log('-> reroute to public main');
next(routePrefix.public);
} else if (to.fullPath.startsWith(routePrefix.public) &&
Store.getters['auth/isAuthenticated']) {
console.log('-> reroute to private main');
next(routePrefix.private);
} else {
next();
}
});
如果您想知道路线前缀是什么样的,请转到:
const routePrefix = {
public: '/p', private: '/x',
};
没有什么特别的 .
问题
我打开页面 localhost:8080/
,按预期将 /
重定向到 /p/login
. 成功登录后,我执行了 Router.push('/')
,目的是再次进一步重新路由用户 .
这个想法是 /
应该再次重定向到 /p/login
,导航警卫开始并将其重定向到 /x/
......但事实并非如此 . 它保持在 /
.
是不是应该将其重定向远离主页?我该如何解决?
1 回答
我没有找到解决这个问题的方法 . 我已经达到了我的目标,直接推到了理想的目的地,而不是让重新路由发挥其魔力 . 这有效 .