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

ppylox
·8/6/2021·0 阅读
AI 摘要模型根据正文自动生成,仅供参考

新版Vue Router用router.addRoute替代addRoutes动态添加路由。添加子路由时,调用addRoute传入父路由name和子路由对象;子路由path必须包含父路由完整路径,如父路由/index下添加test,path应写为index/test,否则匹配失败。

在添加子路由的时候

比如原现有路由

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

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

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

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n

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

相关文章

评论 (0)

暂无评论,来写第一条吧