react的ant-design-pro框架左侧菜单修改为动态路由

[复制链接]
发表于 2025-9-20 01:51:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
       在使用 React 框架联合 Ant Design Pro 举行项目开发时,动态路由的修改是一项常见且紧张的使命。动态路由可以或许根据用户的角色、权限大概其他运行时的条件来展示差别的页面内容,极大地提拔了应用的灵活性和安全性。本文将联合一个完备的示例项目,详细先容如何在 Ant Design Pro 中修改动态路由。  
在 Ant Design Pro 中使用动态路由的好处包罗:

  • 灵活性:可以根据条件动态加载路由,适应差别的需求。
  • 性能优化:联合懒加载淘汰初次加载时间,进步性能
  • 可维护性:路由和菜单管理清楚,易于管理和维护。
  • 权限控制:可以根据用户角色控制访问权限,加强安全性。
  • 用户体验:通过动态路由改善页面跳转体验,进步流畅性
动态路由都必要先在routes中注册页面,然后通过后端对比举行左侧菜单的表现。
下面又开看详细步调吧!
一. 路由配置(通过access来举行权限验证)。在routes.ts中注册页面路由。
  1. export default [
  2.   {
  3.     path: '/user',
  4.     layout: false,
  5.     routes: [
  6.       {
  7.         name: 'login',
  8.         path: '/user/login',
  9.         component: './User/Login',
  10.       },
  11.     ],
  12.   },
  13.   // 首页
  14.   {
  15.     path: '/welcome',
  16.     name: 'welcome',
  17.     icon: 'HomeOutlined',
  18.     component: './Welcome',
  19.     layout: true,
  20.     hideTitle: false,
  21.   },
  22.   // 门诊
  23.   {
  24.     path: '/admin',
  25.     name: 'admin',
  26.     icon: 'table',
  27.     hideInBreadcrumb: true, // 添加这行,可以去掉父元素带子级的面包屑
  28.     routes: [
  29.       // 这第一个对象的作用就是当点击父级菜单时,默认重定向到父级下的第一个子菜单页面
  30.       {
  31.         path: '/admin',
  32.         redirect: '/admin/sub-page',
  33.       },
  34.       // 门诊缴费记录
  35.       {
  36.         path: '/admin/sub-page',
  37.         name: '门诊缴费记录',
  38.         layout: false,
  39.         component: './outpatient',
  40.       },
  41.       // 预约记录
  42.       {
  43.         path: '/admin/subscribe',
  44.         name: '预约记录',
  45.         // icon:'SnippetsFilled',
  46.         layout: false,
  47.         component: './Subscribe',
  48.       },
  49.     ],
  50.   },
  51.   // 住院
  52.   {
  53.     name: 'list',
  54.     icon: 'ReconciliationFilled',
  55.     hideInBreadcrumb: true, // 添加这行,可以去掉父元素带子级的面包屑
  56.     path: '/list',
  57.     routes: [
  58.       // 这第一个对象的作用就是当点击父级菜单时,默认重定向到父级下的第一个子菜单页面
  59.       {
  60.         path: '/list',
  61.         redirect: '/list/hospital',
  62.         layout: false,
  63.       },
  64.       // 住院缴费记录
  65.       {
  66.         path: '/list/hospital',
  67.         name: '住院缴费记录',
  68.         component: './HospitalPrice',
  69.         layout: false,
  70.       },
  71.       // 房间管理
  72.       {
  73.         path: '/list/room',
  74.         name: '房间管理',
  75.         component: './Room',
  76.         layout: false,
  77.       },
  78.     ],
  79.   },
  80.   // 用户管理
  81.   {
  82.     name: 'user',
  83.     icon: 'SnippetsFilled',
  84.     hideInBreadcrumb: true, // 添加这行,可以去掉父元素带子级的面包屑
  85.     path: '/user',
  86.     routes: [
  87.       // 这第一个对象的作用就是当点击父级菜单时,默认重定向到父级下的第一个子菜单页面
  88.       {
  89.         path: '/user/user',
  90.         // redirect: '/user/user',
  91.         name: '用户管理',
  92.         component: './user',
  93.         layout: false,
  94.       },
  95.       // 角色管理
  96.       {
  97.         path: '/user/role',
  98.         name: '角色管理',
  99.         component: './role',
  100.         layout: false,
  101.       },
  102.       // 管理员管理
  103.       {
  104.         path: '/user/admin',
  105.         name: '管理员管理',
  106.         component: './Manage',
  107.         layout: false,
  108.       },
  109.       // 就诊卡
  110.       {
  111.         path: '/user/card',
  112.         name: '就诊卡',
  113.         component: './Cards',
  114.         layout: false,
  115.       },
  116.     ],
  117.   },
  118.   // 科室管理
  119.   {
  120.     name: '科室管理',
  121.     path: '/TableList',
  122.     icon: 'DiffFilled',
  123.     access: 'adminRouteFilter',
  124.     component: './TableList',
  125.     layout: true,
  126.   },
  127.   // 医院信息
  128.   {
  129.     name: 'hospital',
  130.     icon: 'MedicineBoxFilled',
  131.     hideInBreadcrumb: true, // 添加这行,可以去掉父元素带子级的面包屑
  132.     path: '/hospital',
  133.     routes: [
  134.       // 这第一个对象的作用就是当点击父级菜单时,默认重定向到父级下的第一个子菜单页面
  135.       {
  136.         path: '/hospital',
  137.         redirect: '/hospital/massage',
  138.       },
  139.       // 医生信息
  140.       {
  141.         path: '/hospital/massage',
  142.         name: '医院信息',
  143.         // icon:'SmileFilled',
  144.         component: './HospitalMassage',
  145.         layout: false,
  146.       },
  147.       // 医生管理
  148.       {
  149.         path: '/hospital/doctor',
  150.         name: '医生管理',
  151.         component: './DoctorOffice',
  152.         layout: false,
  153.       },
  154.       // 排班管理
  155.       {
  156.         name: '医生排班管理',
  157.         // icon: 'table',
  158.         path: '/hospital/workforce',
  159.         component: './workforce',
  160.         layout: false,
  161.       },
  162.       // 标签管理
  163.       {
  164.         name: '标签管理',
  165.         // icon: 'table',
  166.         path: '/hospital/label',
  167.         access: 'adminRouteFilter',
  168.         component: './label',
  169.         layout: false,
  170.       },
  171.       // 体检管理
  172.       {
  173.         name: '体检管理',
  174.         // icon: 'table',
  175.         path: '/hospital/physical',
  176.         component: './physical',
  177.         layout: false,
  178.       },
  179.       // 药品管理
  180.       {
  181.         name: '药品管理',
  182.         // icon: 'table',
  183.         path: '/hospital/drug',
  184.         component: './drug',
  185.         layout: false,
  186.       },
  187.       // 医院动态
  188.       {
  189.         name: '医院动态',
  190.         // icon: 'table',
  191.         path: '/hospital/dynamic',
  192.         component: './dynamic',
  193.         layout: false,
  194.       },
  195.     ],
  196.   },
  197.   {
  198.     path: '/',
  199.     redirect: '/welcome',
  200.   },
  201.   {
  202.     path: '*',
  203.     layout: false,
  204.     component: './404',
  205.   },
  206. ];
复制代码
二、动态路由实现步调
 1:配置路由访问权限(在routes.ts中给必要权限表现的路由添加:access)
  1. // 科室管理
  2.   {
  3.     name: '科室管理',
  4.     path: '/TableList',
  5.     icon: 'DiffFilled',
  6.     access: 'adminRouteFilter',
  7.     component: './TableList',
  8.     layout: true,
  9.   },
复制代码
2:权限控制(在access.ts中誊写,必要根据本身的path举行修改)
  1. // 动态路由控制
  2. const adminRoutes = ['user', 'welcome', 'admin', 'list', 'TableList', 'hospital'];
  3. const normalRoutes = ['user', 'list', 'TableList', 'hospital'];
  4. const allUserRoutes = ['welcome'];
  5. export default function (initialState = {}) {
  6.   // 取的当前账号
  7.   const userInfo = JSON.parse(localStorage.getItem('user_account'));
  8.   console.log('userInfo', userInfo);
  9.   const isAdmin = userInfo === 'admin';
  10.   const hasRoutes = isAdmin ? adminRoutes : normalRoutes;
  11.   return {
  12.     adminRouteFilter: () => isAdmin, // 只有管理员可访问
  13.     normalRouteFilter: (route) => hasRoutes.includes(route.name), // initialState 中包含了的路由才有权限访问
  14.     allUserRouteFilter: (route) => allUserRoutes.includes(route.name),
  15.   };
  16. }
复制代码
分析权限控制代码:
1. 声明变量即是可以访问的菜单
  1. const adminRoutes = ['user', 'welcome', 'admin', 'list', 'TableList', 'hospital'];
  2. const normalRoutes = ['user', 'list', 'TableList', 'hospital'];
  3. const allUserRoutes = ['welcome'];
复制代码
2. 在登录页 存用户信息,用于对比
  1. const userInfo = JSON.parse(localStorage.getItem('user_account'));
  2.         console.log('userInfo', userInfo);
复制代码
3. 在access.ts中取出来
  1. // 取的当前账号
  2.   const userInfo = JSON.parse(localStorage.getItem('user_account'));
  3.   console.log('userInfo', userInfo);
复制代码
4.  对比 可以访问的菜单
  1. export default function (initialState = {}) {  
  2. // 取的当前账号
  3.   const userInfo = JSON.parse(localStorage.getItem('user_account'));
  4.   console.log('userInfo', userInfo);  const isAdmin = userInfo === 'admin';  const hasRoutes = isAdmin ? adminRoutes : normalRoutes;  return {    adminRouteFilter: () => isAdmin, // 只有管理员可访问    normalRouteFilter: (route) => hasRoutes.includes(route.name), // initialState 中包罗了的路由才有权限访问    allUserRouteFilter: (route) => allUserRoutes.includes(route.name),  };}
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表