【发布时间】:2021-08-17 20:58:46
【问题描述】:
我想知道如何有条件地重定向子路由。我尝试了很多方法,但没有一个有效。
我使用商店中的状态作为重定向子路由的条件。我已经从它所在的文件中导出了我的商店并将其导入到 routes.js 中:
从“@/store”导入商店
这里是处理路由的代码:
{
path: '/import',
component: Layout,
redirect: '/import/table',
name: 'Import',
meta: { title: 'Import', icon: 'el-icon-right' },
children: [
{
path: 'tree',
name: 'Tree',
component: () =>
import('@/views/tree/import'),
beforeEach: (to,from,next) => {
//store.state.userRole = 'Admin' redirect the route, otherwise not redirect.
if(store.state.userRole = 'Admin') {
next();
}
else {
next(false);
}
meta: { title: 'Create form', icon: 'el-icon-circle-plus-outline' },
},
{
path: 'table',
name: 'Table',
component: () => import('@/views/table/importList'),
meta: { title: 'List', icon: 'el-icon-document' },
},
]
}
【问题讨论】:
标签: javascript vue.js vuejs2 vuex vue-router