【问题标题】:Conditional route for children routes in VueJSVueJS 中子路由的条件路由
【发布时间】: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


    【解决方案1】:

    每个路由保护的正确名称是beforeEnter - 而不是beforeEachhttps://router.vuejs.org/guide/advanced/navigation-guards.html#per-route-guard

    【讨论】:

    • 哎呀我的坏!非常感谢你帮助我
    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 2018-10-05
    • 2022-01-18
    • 2019-01-14
    • 2017-11-07
    相关资源
    最近更新 更多