Vue3动态添加路由及生成菜单

举报
青年码农 发表于 2022/08/24 23:55:04 2022/08/24
【摘要】 前面讲了Vue2项目中动态添加路由及生成菜单,今天尝试在Vue3中动态添加路由及生成菜单。 最近在尝试用Vue3开发个管理平台项目,一切都是从头开始,基本框架搭建,熟悉Vue3写法,编写登录页,编写路由守卫,上面功能已基本完成,开始编写首页布局,用Vue3就必须用Router4.x版本,所以之前的代码迁移过来之后发现,...

前面讲了Vue2项目中动态添加路由及生成菜单,今天尝试在Vue3中动态添加路由及生成菜单。

最近在尝试用Vue3开发个管理平台项目,一切都是从头开始,基本框架搭建,熟悉Vue3写法,编写登录页,编写路由守卫,上面功能已基本完成,开始编写首页布局,用Vue3就必须用Router4.x版本,所以之前的代码迁移过来之后发现,动态路由不生效,查了很多资料,最后发现,Router4中,去掉了 router.addRoutes ,只能使用 addRoute 

21987f2829d3f43ca337e51f41287895.png

所以之前的写法就要相应的调整,之前是可以动态添加更多的路由规则,参数必须是一个符合 routes 选项要求的数组。

router.addRoutes(routes: Array<RouteConfig>);
  

现在是只能添加一个


   
  1. router.addRoute("名称", {
  2.   path: `/index`,
  3.   name: '首页',
  4.   component: () => import(`@/index.vue`)
  5. });

接下来就详细说明

1 路由数据封装

前台把路由写在代码里,这种方式只适用部分情况,所以大部分情况是路由后台提供,比如返回格式如下:


   
  1. {
  2.     "code"0,
  3.     "msg""success",
  4.     "data": [{
  5.         "id"1000,
  6.         "parentId"-1,
  7.         "icon""iconquanxian",
  8.         "name""组织架构",
  9.         "path""/system",
  10.         "component""Layout",
  11.         "redirect": null,
  12.         "type""0",
  13.         "children": [{
  14.             "id"1100,
  15.             "parentId"1000,
  16.             "children": [],
  17.             "icon""iconyonghuguanli",
  18.             "name""用户管理",
  19.             "path""/user",
  20.             "component""views/system/user/index",
  21.             "redirect": null,
  22.             "type""0",
  23.         }],
  24.     }, {
  25.         "id"2000,
  26.         "parentId"-1,
  27.         "icon""iconquanxian",
  28.         "name""权限管理",
  29.         "path""/organization",
  30.         "component""Layout",
  31.         "redirect": null,
  32.         "type""0",
  33.         "children": [{
  34.             "id"2100,
  35.             "parentId"2000,
  36.             "children": [],
  37.             "icon""iconyonghuguanli",
  38.             "name""菜单管理",
  39.             "path""/menu",
  40.             "component""views/system/user/index",
  41.             "redirect": null,
  42.             "type""0",
  43.         }],
  44.     }]
  45. }

这种是后台树型结构返回,前台不需要进行二次处理可以直接显示成菜单,


   
  1. <a-menu
  2.   theme="dark"
  3.   mode="inline"
  4. >
  5.   <a-sub-menu v-for="subitem in menuData.menu" :key="subitem.path">
  6.     <template #title>
  7.       <Icon-font :type="subitem.icon" />
  8.       <span>{{ subitem.name }}</span>
  9.     </template>
  10.     <a-menu-item v-for="item in subitem.children" :key="item.path">{{
  11.       item.name
  12.     }}</a-menu-item>
  13.   </a-sub-menu>
  14. </a-menu>

但是路由需要重新封装,先说说用到的字段,path-路由地址、component这个现在有两种,一种是Layout代表父菜单,另一种views开头的是组件地址。那么我们就可以开始动态生成路由了,写法和Vue2项目有所不同,首先定义一个方法,


   
  1. const routerPackag = routers => {
  2.   routers.filter(itemRouter => {
  3.     if (itemRouter.component != "Layout") {
  4.       router.addRoute("BasicLayout", {
  5.         path: `${itemRouter.path}`,
  6.         name: itemRouter.name,
  7.         component: () => import(`@/${itemRouter.component}`)
  8.       });
  9.     }
  10.     // 是否存在子集
  11.     if (itemRouter.children && itemRouter.children.length) {
  12.       routerPackag(itemRouter.children);
  13.     }
  14.     return true;
  15.   });
  16. };

2 调用

上面这个方式是动态生成路由,接下来就是调用这个方法。


   
  1. getBasisMenu().then(res => {
  2.   if (res.code == 0) {
  3.     routerPackag(res.data);
  4.   }
  5. });

3 效果

动态路由实现了,但是现在还有部分问题未解决

86572a0519488ea26e2cd4cd94d0f5f9.gif

代码在gitee上,可以直接运行。

文章来源: blog.csdn.net,作者:NMGWAP,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/NMGWAP/article/details/125066990

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。