From 86eb38220dec6b6108d446eab0404d4dc052e718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=82=A0=E9=9D=99=E8=90=9D=E8=8E=89?= Date: Tue, 2 Aug 2022 18:03:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0expanded=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=EF=BC=8C=E4=BB=A4=E8=8F=9C=E5=8D=95=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=89=93=E5=BC=80=20(#245)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layouts/components/SideNav.tsx | 6 ++++-- src/router/index.ts | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/layouts/components/SideNav.tsx b/src/layouts/components/SideNav.tsx index 7a6e74e..b4ee42c 100644 --- a/src/layouts/components/SideNav.tsx +++ b/src/layouts/components/SideNav.tsx @@ -1,12 +1,13 @@ import { defineComponent, PropType, computed, onMounted } from 'vue'; import { useRouter } from 'vue-router'; +import union from 'lodash/union'; import { prefix } from '@/config/global'; import pgk from '../../../package.json'; import MenuContent from './MenuContent'; import AssetLogo from '@/assets/assets-t-logo.svg?component'; import AssetLogoFull from '@/assets/assets-logo-full.svg?component'; import { useSettingStore } from '@/store'; -import { getActive } from '@/router'; +import { getActive, getRoutesExpanded } from '@/router'; const MIN_POINT = 992 - 1; @@ -18,7 +19,8 @@ const useComputed = (props) => { const defaultExpanded = computed(() => { const path = getActive(); const parentPath = path.substring(0, path.lastIndexOf('/')); - return parentPath === '' ? [] : [parentPath]; + const expanded = getRoutesExpanded(); + return union(expanded, parentPath === '' ? [] : [parentPath]); }); const sideNavCls = computed(() => { diff --git a/src/router/index.ts b/src/router/index.ts index 09ad02b..4f0725d 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,4 +1,5 @@ import { useRoute, createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'; +import uniq from 'lodash/uniq'; // 自动导入modules文件夹下所有ts文件 const modules = import.meta.globEager('./modules/**/*.ts'); @@ -38,6 +39,25 @@ const defaultRouterList: Array = [ export const allRoutes = [...defaultRouterList, ...asyncRouterList]; +export const getRoutesExpanded = () => { + const expandedRoutes = []; + + allRoutes.forEach((item) => { + if (item.meta && item.meta.expanded) { + expandedRoutes.push(item.path); + } + if (item.children && item.children.length > 0) { + item.children + .filter((child) => child.meta && child.meta.expanded) + .forEach((child: RouteRecordRaw) => { + expandedRoutes.push(item.path); + expandedRoutes.push(`${item.path}/${child.path}`); + }); + } + }); + return uniq(expandedRoutes); +}; + export const getActive = (maxLevel = 3): string => { const route = useRoute(); if (!route.path) {