如何将父(嵌套)路由重定向到特定URL?

时间:2019-10-15 06:37:20

标签: vue.js

我创建了3条父路由,以根据其用户角色ID(共有3个)嵌套各种页面。

这是成功的,我使父级路由显示了差异。他们的子页面上的导航栏。

我的问题是,尽管路由保护器工作得很好,但是每当我转到父页面(例如... / admin /)时,它都会显示空白页面和导航栏。

我想发生的是,当我强制URL转到... / admin /时,它将重定向我到/ dashboard/。

router.js

import Vue from 'vue'
import Router from 'vue-router'
import store from './store'

import...

import User from './views/User/User.vue'
import Admin from './views/Admin/Admin.vue'

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
{
      path: "/user",
      name: "user",
      component: User,
      beforeEnter(to, from,next) {
        if(store.state.currentUser.role_id == 1) {
          next();
        } else {
          next('/');
        }
      },
      children: [
        {
          path: "newproject",
          name: "NewProject",
          component: NewProject
        },
        {
          path: "Projects",
          name: "Projects",
          component: Project
        },
      {
      path: "/admin",
      name: "admin",
      component: Admin,
      beforeEnter(to, from,next) {
        if(store.state.currentUser.role_id == 2) {
          next();
        } else {
          next('/');
        }
      },
      children: [
        {
          path: "accounts",
          name: "Accounts",
          component: Accounts
        } 
      ]
    },
]
})

Admin.vue (与User.vue相同,尽管将“ admin”更改为“ user”

<template>
<div>
    <Navbar/>
    <router-view></router-view>
</div>
</template>

<script>
import Navbar from '@/components/navbar_admin'

export default {

    name: 'Navbar_Admin',
    components: { Navbar },

    }
</script>

谢谢!

0 个答案:

没有答案