新版Vue Router中用router.addRoute来替代原有的router.addRoutes来动态添加路由、子路由

在添加子路由的时候

比如原现有路由

  1. const routes = [
  1. {
  1. path: '/',
  2. name: 'Login',
  3. component: () => import(/* webpackChunkName: "about" */ '@/views/Login.vue')
  1. },
  2. {
  1. path: '/index',
  2. name: 'index',
  3. meta: { title: '首页', noCache: true },
  4. component: () => import(/* webpackChunkName: "about" */ '@/views/index.vue'),
  5. children:[]
  1. }
  2. ]

想要在index下动态添加子路由test,特别要注意添加的子路由的path一定要把父路由的路径也带上

  1. const routeObj = {
  1. path: 'index/test', // 这里要把父路由的路径也带上
  2. name: 'test',
  3. meta: { title: '测试路由test', noCache: true },
  4. component: () =>
  1. import('../test/test.vue'),
  1. }

  1. this.$router.addRoute('index', routeObj)

本文章由javascript技术分享原创和收集

发表评论 (审核通过后显示评论):