mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-11-15 14:03:36 +08:00
feat: 添加菜单树结构事例
This commit is contained in:
42
src/utils/tree.ts
Normal file
42
src/utils/tree.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 提取菜单树中的每一项path
|
||||
* @param {Object} {menuTree 菜单树}
|
||||
* @param {return}} expandedPaths 每一项path组成的数组
|
||||
*/
|
||||
const expandedPaths = [];
|
||||
export function extractPathList(menuTree) {
|
||||
if (!Array.isArray(menuTree)) {
|
||||
console.warn("menuTree must be an array");
|
||||
return;
|
||||
}
|
||||
if (!menuTree || menuTree.length === 0) return;
|
||||
for (const node of menuTree) {
|
||||
const hasChildren = node.children && node.children.length > 0;
|
||||
if (hasChildren) {
|
||||
extractPathList(node.children);
|
||||
}
|
||||
expandedPaths.push(node.path);
|
||||
}
|
||||
return expandedPaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果父级下children的length为1,删除children
|
||||
* @param {Object} {menuTree 菜单树}
|
||||
* @param {return}}
|
||||
*/
|
||||
export function deleteChildren(menuTree) {
|
||||
if (!Array.isArray(menuTree)) {
|
||||
console.warn("menuTree must be an array");
|
||||
return;
|
||||
}
|
||||
if (!menuTree || menuTree.length === 0) return;
|
||||
for (const node of menuTree) {
|
||||
if (node.children && node.children.length === 1) delete node.children;
|
||||
const hasChildren = node.children && node.children.length > 0;
|
||||
if (hasChildren) {
|
||||
deleteChildren(node.children);
|
||||
}
|
||||
}
|
||||
return menuTree;
|
||||
}
|
||||
Reference in New Issue
Block a user