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) {