mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-06-08 01:17:23 +08:00
perf: menu tree
This commit is contained in:
parent
10fa0ee8c8
commit
0903008ced
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* 提取菜单树中的每一项path
|
* 提取菜单树中的每一项uniqueId
|
||||||
* @param {Object} {menuTree 菜单树}
|
* @param {Array} {menuTree 菜单树}
|
||||||
* @param {return}} expandedPaths 每一项path组成的数组
|
* @param {return}} expandedPaths 每一项uniqueId组成的数组
|
||||||
*/
|
*/
|
||||||
const expandedPaths = [];
|
const expandedPaths = [];
|
||||||
export function extractPathList(menuTree) {
|
export function extractPathList(menuTree) {
|
||||||
@ -15,27 +15,33 @@ export function extractPathList(menuTree) {
|
|||||||
if (hasChildren) {
|
if (hasChildren) {
|
||||||
extractPathList(node.children);
|
extractPathList(node.children);
|
||||||
}
|
}
|
||||||
expandedPaths.push(node.path);
|
expandedPaths.push(node.uniqueId);
|
||||||
}
|
}
|
||||||
return expandedPaths;
|
return expandedPaths;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果父级下children的length为1,删除children
|
* 如果父级下children的length为1,删除children并自动组建唯一uniqueId
|
||||||
* @param {Object} {menuTree 菜单树}
|
* @param {Array} {menuTree 菜单树}
|
||||||
|
* @param {Array} {pathList 每一项的id组成的数组}
|
||||||
* @param {return}}
|
* @param {return}}
|
||||||
*/
|
*/
|
||||||
export function deleteChildren(menuTree) {
|
export function deleteChildren(menuTree, pathList = []) {
|
||||||
if (!Array.isArray(menuTree)) {
|
if (!Array.isArray(menuTree)) {
|
||||||
console.warn("menuTree must be an array");
|
console.warn("menuTree must be an array");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!menuTree || menuTree.length === 0) return;
|
if (!menuTree || menuTree.length === 0) return;
|
||||||
for (const node of menuTree) {
|
for (const [key, node] of menuTree.entries()) {
|
||||||
if (node.children && node.children.length === 1) delete node.children;
|
if (node.children && node.children.length === 1) delete node.children;
|
||||||
|
node.id = key;
|
||||||
|
node.parentId = pathList.length ? pathList[pathList.length - 1] : null;
|
||||||
|
node.pathList = [...pathList, node.id];
|
||||||
|
node.uniqueId =
|
||||||
|
node.pathList.length > 1 ? node.pathList.join("-") : node.pathList[0];
|
||||||
const hasChildren = node.children && node.children.length > 0;
|
const hasChildren = node.children && node.children.length > 0;
|
||||||
if (hasChildren) {
|
if (hasChildren) {
|
||||||
deleteChildren(node.children);
|
deleteChildren(node.children, node.pathList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return menuTree;
|
return menuTree;
|
||||||
|
@ -10,7 +10,7 @@ import { extractPathList, deleteChildren } from "/@/utils/tree";
|
|||||||
import { usePermissionStoreHook } from "/@/store/modules/permission";
|
import { usePermissionStoreHook } from "/@/store/modules/permission";
|
||||||
|
|
||||||
let dataProps = ref({
|
let dataProps = ref({
|
||||||
value: "path",
|
value: "uniqueId",
|
||||||
children: "children"
|
children: "children"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user