react ant ice3 实现点击一级菜单自动打开它下面最深的第一个子菜单

1.问题

默认的如果没有你的菜单结构是这样的:

js 复制代码
[
	{
	children: [
		{
			name: "通用配置"
			parentId: "1744857774620672"
			path: "basic"
		}
	],
	name: "系统管理"
	parentId: "-1"
	path: "system"
	}
]

可以看到每层菜单的path都只有当前的路径,没有进行全拼接。

那么此时点击一级菜单就会有问题:

2.解决

让一级菜单进行全拼接,二三级等子菜单不做处理,仍然保持当前的path,也就是点击一级菜单自动打开它下面最深的第一个子菜单。

js 复制代码
/**
 * @description: 递归 设置一个父亲菜单完整的菜单路径,
 * 例如:[{path:home,children;[{path:console}]}]
 * 得到:[{path:home/console,children;[{path:console}]}]
 * @param menuData 递归的对象
 * @param parentPath 父路径
 * @returns 替换后的对象
 */
export const setParentPaths = (data) => {
  if (data.length === 0) return;
  data.forEach((i) => {
    const concatPaths = (item, parentPath = "") => {
      const currentPath = parentPath + "/" + (item.path || item.fullPath);

      if (item.children && item.children.length > 0) {
        item.fullPath = concatPaths(item.children[0], currentPath);
      } else {
        item.fullPath = currentPath;
      }

      return item.fullPath;
    };

    concatPaths(i);
  });

  return data;
};
相关推荐
网络点点滴1 天前
customRef的强大之处
开发语言·前端·javascript
小茴香3531 天前
拖拽实现(原生JS+Vue)
前端·javascript·vue.js·typescript
清风徐来QCQ1 天前
js中的常用api
开发语言·javascript·ecmascript
不会写DN1 天前
Gin 日志体系详解
前端·javascript·gin
冬夜戏雪1 天前
实习面经记录(十)
java·前端·javascript
爱学习的程序媛1 天前
【Web前端】JavaScript设计模式全解析
前端·javascript·设计模式·web
老黑1 天前
开源工具 AIDA:给 AI 辅助开发加一个数据采集层,让 AI 从错误中自动学习(Glama 3A 认证)
前端·react.js·ai·nodejs·cursor·vibe coding·claude code
薛先生_0991 天前
js学习语法第一天
开发语言·javascript·学习
苦瓜小生1 天前
【前端】|【js手撕】经典高频面试题:手写实现function.call、apply、bind
java·前端·javascript
和沐阳学逆向1 天前
我现在怎么用 CC Switch 管中转站,顺手拿 Codex 举个例子
开发语言·javascript·ecmascript