【发布时间】:2019-04-27 08:36:26
【问题描述】:
我和这里的其他人一样有共同的问题,我尝试遵循他们的解决方案,但我仍然无法解决。在我的应用程序中,我需要先登录,然后选择公司,然后再进入主页。我的登录没有问题,我的问题出在选定的公司上。如果他们没有选择公司,我需要附加一个可以阻止他们访问主页的警卫。我所做的是检查本地存储,如果 selectCorporation 为空,那么我将能够知道他们选择了一家公司。
const router = new Router({
mode: 'history',
routes: [
{ path: '/', name: 'overview', component: Overview },
// Authentication
{ path: '/auth/login', name: 'auth.login', component: Login, meta: { requiresVisitor: true }},
//Select Corporation
{ path: 'select-corporation', name: 'corporations.select', component: CorporationsSelect }
// Branches
{ path: '/branches', name: 'branches.index', component: BranchesIndex },
{ path: '/branches/create', name: 'branches.create', component: BranchesCreate },
{ path: '/branches/:id', name: 'branches.view', component: BranchesView },
{ path: '/branches/:id/edit', name: 'branches.edit', component: BranchesEdit },
});
router.beforeEach((to, from, next) => {
if (localStorage.getItem('selectedCorporation') === null) {
next({
path: '/select-corporation'
});
} else {
next({
path: '/branches'
});
}
});
export default router;
【问题讨论】:
-
我认为你的守卫总是重定向到
select-corporation,即使目标路由本身是select-corporation。所以这是无限重定向循环。 -
@MaxSinev。是的,我相信这就是问题所在。那么我该如何解决这个问题,我已经设置了 else 条件?谢谢
标签: vue.js vuejs2 vue-component vue-router