perf: menu tree

This commit is contained in:
xiaoxian521 2021-12-16 13:47:13 +08:00
parent 10fa0ee8c8
commit 0903008ced
2 changed files with 16 additions and 10 deletions

View File

@ -1,7 +1,7 @@
/**
* path
* @param {Object} {menuTree }
* @param {return}} expandedPaths path组成的数组
* uniqueId
* @param {Array} {menuTree }
* @param {return}} expandedPaths uniqueId组成的数组
*/
const expandedPaths = [];
export function extractPathList(menuTree) {
@ -15,27 +15,33 @@ export function extractPathList(menuTree) {
if (hasChildren) {
extractPathList(node.children);
}
expandedPaths.push(node.path);
expandedPaths.push(node.uniqueId);
}
return expandedPaths;
}
/**
* children的length为1children
* @param {Object} {menuTree }
* children的length为1children并自动组建唯一uniqueId
* @param {Array} {menuTree }
* @param {Array} {pathList id组成的数组}
* @param {return}}
*/
export function deleteChildren(menuTree) {
export function deleteChildren(menuTree, pathList = []) {
if (!Array.isArray(menuTree)) {
console.warn("menuTree must be an array");
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;
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;
if (hasChildren) {
deleteChildren(node.children);
deleteChildren(node.children, node.pathList);
}
}
return menuTree;

View File

@ -10,7 +10,7 @@ import { extractPathList, deleteChildren } from "/@/utils/tree";
import { usePermissionStoreHook } from "/@/store/modules/permission";
let dataProps = ref({
value: "path",
value: "uniqueId",
children: "children"
});