当前位置: 首页 > news >正文

react - 根据路由生成菜单

后端返回菜单的格式
menuList:[{index: true,name: "",component: "../views/Home",meta: { title: "首页", requiresAuth: true,roles:['user']},},{path: "/admin",name: "admin",meta: { title: "管理页", roles: ["admin"] },children: [{path: "/admin/user",name: "User",component: "@/pages/Admin/User",meta: { title: "用户管理" },children: [{path: "/admin/user",name: "User",component: "@/pages/Admin/User",meta: { title: "用户管理1" },},],},],},],

递归转换 menuList 为 Ant Design Menu 的 items 格式

const convertMenuListToItems = (menuList: any[]): MenuItem[] => {return menuList.map((menu) => {const item: any = {key: menu.path || menu.name, // 使用 path 或 name 作为 keylabel: menu.meta?.title || menu.name, // 显示标题};// 如果有子菜单,递归处理if (menu.children) {item.children = convertMenuListToItems(menu.children);}// 如果是 index 路由,可以标记特殊 key(可选)if (menu.index) {item.key = 'home'; // 强制指定 key,避免重复}return item;});
};const Menu1: React.FC = () => {const { menuList } = useBoundStore()
const items =convertMenuListToItems(menuList)const [current, setCurrent] = useState('mail');const onClick: MenuProps['onClick'] = (e) => {console.log('click ', e);setCurrent(e.key);};return <Menu onClick={onClick} selectedKeys={[current]} mode="horizontal" items={items} style={{display:'flex',justifyContent:'center',alignItems:'center'}}/>;
};export default Menu1;


文章转载自:
http://anthelmintic.tmizpp.cn
http://chalkiness.tmizpp.cn
http://bitmap.tmizpp.cn
http://amuck.tmizpp.cn
http://backstroke.tmizpp.cn
http://allen.tmizpp.cn
http://armamentarium.tmizpp.cn
http://aerogenerator.tmizpp.cn
http://andrology.tmizpp.cn
http://archeological.tmizpp.cn
http://abirritant.tmizpp.cn
http://blinking.tmizpp.cn
http://authentification.tmizpp.cn
http://captainless.tmizpp.cn
http://author.tmizpp.cn
http://aconitic.tmizpp.cn
http://bottomless.tmizpp.cn
http://alamanni.tmizpp.cn
http://anthropology.tmizpp.cn
http://acetifier.tmizpp.cn
http://astropologist.tmizpp.cn
http://bury.tmizpp.cn
http://belabour.tmizpp.cn
http://arbitrational.tmizpp.cn
http://barytron.tmizpp.cn
http://affirmable.tmizpp.cn
http://chiliarch.tmizpp.cn
http://bedcover.tmizpp.cn
http://bumtang.tmizpp.cn
http://barranca.tmizpp.cn
http://www.dtcms.com/a/280457.html

相关文章:

  • 多模态大模型研究每日简报(2025-07-14)
  • AI应用服务
  • 整除分块练习题
  • 某地金属矿山自动化监测服务项目
  • Python 数据建模与分析项目实战预备 Day 6 - 多模型对比与交叉验证验证策略
  • 2.【C# in .NET】探秘数据类型:从底层机制到实战启示
  • MySQL高级篇(二):深入理解数据库事务与MySQL锁机制
  • 农村养老模式:乡土智慧与时代创新的共生之路
  • 【每日算法】专题十_字符串
  • PySpark Standalone 集群
  • react native学习record one month
  • Flink SQL 性能优化实战
  • 使用Dify+fastmcp 实现mcp服务,内含详细步骤与源码
  • Windows远程FX的编解码器性能优化
  • 算法在前端框架中的集成
  • 三十二、【核心功能改造】数据驱动:重构仪表盘与关键指标可视化
  • 原型继承(prototypal inheritance)的工作原理
  • Java实现word、pdf转html保留格式
  • 19.如何将 Python 字符串转换为 Slug
  • 全面安装指南:在Linux、Windows和macOS上部署Apache Cassandra
  • 基于STM32与中航ZH-E3L字符卡通信在LED屏显示数据
  • 华为敏态开发流程敏捷开发费用估算敏态IT财务分析模板
  • 进程探秘:从 PCB 到 fork 的核心原理之旅
  • Lang3
  • Spring Ioc Bean 到底是什么
  • 朝鲜升级供应链恶意软件XORIndex,再次瞄准npm生态系统
  • 从springcloud-gateway了解同步和异步,webflux webMvc、共享变量
  • 四种高效搭建SpringBoot项目的方式详解
  • 基于UDP/IP网络游戏加速高级拥塞控制算法(示意:一)
  • SpringBoot 实现 Redis读写分离