From e8faa26bd76c50cbe8e6ede28cfe97e2fed91266 Mon Sep 17 00:00:00 2001 From: pany <939630029@qq.com> Date: Thu, 15 Jun 2023 13:42:58 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96=20?= =?UTF-8?q?layout/Sidebar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layout/components/Sidebar/SidebarItem.vue | 58 +++++++++---------- src/layout/components/Sidebar/index.vue | 14 ++--- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/layout/components/Sidebar/SidebarItem.vue b/src/layout/components/Sidebar/SidebarItem.vue index f61bc98..a7ee74c 100644 --- a/src/layout/components/Sidebar/SidebarItem.vue +++ b/src/layout/components/Sidebar/SidebarItem.vue @@ -24,44 +24,42 @@ const props = defineProps({ } }) -const alwaysShowRootMenu = computed(() => { - return props.item.meta && props.item.meta.alwaysShow +/** 是否始终显示根菜单 */ +const alwaysShowRootMenu = computed(() => props.item.meta?.alwaysShow) + +/** 显示的子菜单 */ +const showingChildren = computed(() => { + return props.item.children?.filter((child) => !child.meta?.hidden) ?? [] }) +/** 显示的子菜单数量 */ const showingChildNumber = computed(() => { - if (props.item.children) { - const showingChildren = props.item.children.filter((item) => { - return !(item.meta && item.meta.hidden) - }) - return showingChildren.length - } - return 0 + return showingChildren.value.length }) +/** 唯一的子菜单项 */ const theOnlyOneChild = computed(() => { - if (showingChildNumber.value > 1) { - return null + const number = showingChildNumber.value + switch (true) { + case number > 1: + return null + case number === 1: + return showingChildren.value[0] + default: + return { ...props.item, path: "" } } - if (props.item.children) { - for (const child of props.item.children) { - if (!child.meta || !child.meta.hidden) { - return child - } - } - } - // If there is no children, return itself with path removed, - // because this.basePath already contains item's path information - return { ...props.item, path: "" } }) +/** 解析路径 */ const resolvePath = (routePath: string) => { - if (isExternal(routePath)) { - return routePath + switch (true) { + case isExternal(routePath): + return routePath + case isExternal(props.basePath): + return props.basePath + default: + return path.resolve(props.basePath, routePath) } - if (isExternal(props.basePath)) { - return props.basePath - } - return path.resolve(props.basePath, routePath) } @@ -80,9 +78,9 @@ const resolvePath = (routePath: string) => {