From 66ab892739410718f0ad42a1a6e384d7c2d36b67 Mon Sep 17 00:00:00 2001 From: loicduong Date: Sat, 7 Sep 2024 15:17:19 +0700 Subject: [PATCH] optimize(core): optimize menu selectedKey --- src/layouts/context/index.ts | 25 ++++++++++++++----- .../global-menu/modules/horizontal-menu.vue | 11 ++------ .../modules/horizontal-mix-menu.vue | 14 +++-------- .../modules/reversed-horizontal-mix-menu.vue | 14 +++-------- .../global-menu/modules/vertical-menu.vue | 11 ++------ .../global-menu/modules/vertical-mix-menu.vue | 12 ++------- 6 files changed, 31 insertions(+), 56 deletions(-) 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()