vue router嵌套路由配置规则,path赋值错误导致404问题

前端同学使用vue肯定会用到 vue-router,在开发后台管理系统时也肯定会用到嵌套路由

那么配置嵌套路由时,一些小伙伴经常遇到奇葩问题(找不到原因的问题,都属于奇葩问题,哈哈哈哈),path写的对,使用的对,但是却自动拦截到404 上

404页面

{
    path: '/page',
    // Main  为嵌套的组件
    component: Main,
    children: [
      {
        path: '/404',
        name: '404',
        component: () =>
          import(/* webpackChunkName: "error" */ '@/layout/error.vue')
      }
    ]
  },
  {
    path: '/:pathMatch(.*)',
    redirect: '/404'
  }

第一种写法

const page = {
  path: '/index',
  name: 'index',
  component: Main,
  children: [
    {
      path: 'role',
      name: 'role',
      component: () =>
        import(
          /* webpackChunkName: "role" */ '@/views/index/role.vue'
        ),
      meta: {
        title: '角色'
      }
    }
  ]
};

export default page;


// 使用该路径
$router.push('/index/role')

// 浏览器URL
http://xxx.xxx.com/index/role ;

第二种

const page = {
  path: '/index',
  name: 'index',
  component: Main,
  children: [
    {
      path: '/index/role',
      name: 'role',
      component: () =>
        import(
          /* webpackChunkName: "role" */ '@/views/index/role.vue'
        ),
      meta: {
        title: '角色'
      }
    }
  ]
};

export default page;


// 使用该路径
$router.push('/index/role')

// 浏览器URL
http://xxx.xxx.com/index/role ;

第三种(不推荐)



const page = {
  path: '',
  name: 'index',
  component: Main,
  children: [
    {
      path: 'index/role',
      name: 'role',
      component: () =>
        import(
          /* webpackChunkName: "role" */ '@/views/index/role.vue'
        ),
      meta: {
        title: '角色'
      }
    }
  ]
};

export default page;


// 使用该路径
$router.push('/index/role')

// 浏览器URL
http://xxx.xxx.com/index/role ;

第四种(错误)

const page = {
  path: '/index',
  name: 'index',
  component: Main,
  children: [
    {
      path: '/role',
      name: 'role',
      component: () =>
        import(
          /* webpackChunkName: "role" */ '@/views/index/role.vue'
        ),
      meta: {
        title: '角色'
      }
    }
  ]
};

export default page;


// 使用该路径
$router.push('/role')

// 浏览器URL
http://xxx.xxx.com/role ;

第五种(错误)

const page = {
  path: '/index',
  name: 'index',
  component: Main,
  children: [
    {
      path: 'index/role',
      name: 'role',
      component: () =>
        import(
          /* webpackChunkName: "role" */ '@/views/index/role.vue'
        ),
      meta: {
        title: '角色'
      }
    }
  ]
};

export default page;


// 使用该路径
$router.push('/index/role')

// 浏览器URL
http://xxx.xxx.com/index/index/role ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 2 的嵌套路由可以帮助我们构建复杂的页面结构。在嵌套路由中,一个路由可以包含多个子路由,而每个子路由又可以再次包含自己的子路由。 下面是一个示例,展示了如何在 Vue 2 中使用嵌套路由: 首先,我们需要设置主路由(父路由),这可以在 `router/index.js` 文件中完成: ```javascript import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) export default new Router({ routes: [ { path: '/', component: Home, children: [ { path: 'about', component: About, children: [ { path: 'contact', component: Contact }, // 其他子路由... ] }, // 其他子路由... ] } ] }) ``` 在上面的示例中,我们定义了一个主路由 `/`,它的组件是 `Home`。然后,我们定义了一个子路由 `/about`,它的组件是 `About`。在 `About` 组件中,又定义了一个子路由 `/contact`,它的组件是 `Contact`。 现在,我们可以在 `Home` 组件的模板中添加一个 `<router-view>` 标签来展示嵌套路由的内容: ```html <template> <div> <h1>Home</h1> <router-view></router-view> </div> </template> ``` 同样地,在 `About` 组件的模板中也要添加一个 `<router-view>` 标签: ```html <template> <div> <h2>About</h2> <router-view></router-view> </div> </template> ``` 这样,当我们访问 `/about` 路径时,`Home` 组件会显示出来,并且在其中展示 `About` 组件。 当访问 `/about/contact` 路径时,`Home` 和 `About` 组件都会显示出来,并且在 `About` 组件中展示 `Contact` 组件。 希望这个示例对你有所帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值