diff --git a/src/layouts/context/index.ts b/src/layouts/context/index.ts index e530580..475fc04 100644 --- a/src/layouts/context/index.ts +++ b/src/layouts/context/index.ts @@ -8,6 +8,7 @@ export const { setupStore: setupMixMenuContext, useStore: useMixMenuContext } = function useMixMenu() { const route = useRoute() const routeStore = useRouteStore() + const { selectedKey } = useMenu() const activeFirstLevelMenuKey = ref('') @@ -16,12 +17,7 @@ function useMixMenu() { } function getActiveFirstLevelMenuKey() { - const { hideInMenu, activeMenu } = route.meta - const name = route.name as string - - const routeName = (hideInMenu ? activeMenu : name) || name - - const [firstLevelRouteName] = routeName.split('_') + const [firstLevelRouteName] = selectedKey.value.split('_') setActiveFirstLevelMenuKey(firstLevelRouteName) } @@ -68,3 +64,20 @@ function useMixMenu() { getActiveFirstLevelMenuKey, } } + +export function useMenu() { + const route = useRoute() + + const selectedKey = computed(() => { + const { hideInMenu, activeMenu } = route.meta + const name = route.name as string + + const routeName = (hideInMenu ? activeMenu : name) || name + + return routeName + }) + + return { + selectedKey, + } +} diff --git a/src/layouts/modules/global-menu/modules/horizontal-menu.vue b/src/layouts/modules/global-menu/modules/horizontal-menu.vue index 6ec8b52..11daf81 100644 --- a/src/layouts/modules/global-menu/modules/horizontal-menu.vue +++ b/src/layouts/modules/global-menu/modules/horizontal-menu.vue @@ -2,23 +2,16 @@ import { GLOBAL_HEADER_MENU_ID } from '@/constants/app' import { useRouterPush } from '@/hooks/common/router' import { useRouteStore } from '@/store/modules/route' +import { useMenu } from '../../../context' defineOptions({ name: 'HorizontalMenu', }) -const route = useRoute() const routeStore = useRouteStore() const { routerPushByKeyWithMetaQuery } = useRouterPush() -const selectedKey = computed(() => { - const { hideInMenu, activeMenu } = route.meta - const name = route.name as string - - const routeName = (hideInMenu ? activeMenu : name) || name - - return routeName -}) +const { selectedKey } = useMenu()