问题描述
根据下面的路由配置,如果用户未通过身份验证,我的应用程序应重定向到“ / login”:
> sum(sapply(1:20,function(i) sapply(1:10,function(j) i**2/(5+j**3))))
[1] 886.0118
当我导航到路径“ /”或“ / other”时,它运行良好。 但是当我导航到路径“ / demand-history / 1 / all / all / all”时,我被重定向到“ / demand-history / 1 / all / all / login”
使用const ifAuthenticated = (to,from,next) => {
if(Store.getters.isAuthenticated) {
next();
return;
}
else
{
next({ path: '/login' });
}
}
export default new Router({
routes: [
{
path: '/login',name: 'Login',component: DsLogin
},{
path: '/',name: 'home',component: DsHome,beforeEnter: (to,next) => {
ifAuthenticated(to,next);
},},{
path: '/other',name: 'other',component: DsOther,{
path: '/demand-history',name: 'demand history',component: DsDemandHistory,redirect: '/demand-history/1/all/all/all',children: [
{
path: ':page/:type/:state/:owner',name: 'demand history filtered',props: true,next) => {
ifAuthenticated(to,next);
}
}
]
}
]
})
也不起作用
我也应该如何设法重定向到'/ login'?
Thx
解决方法
重定向不是通过方法ifAuthenticated
发起的,而是在代码的上游进行的。
拦截器捕获到401错误,并使用Router.push('login')
重定向到登录名
用Router.push({ name: 'Login' })
更改代码解决了我的问题。