From a9c98d9655a363ecffd8e6608b63722a2c8c0b5b Mon Sep 17 00:00:00 2001 From: Azir <79054161+Azir-11@users.noreply.github.com> Date: Tue, 6 Feb 2024 17:42:51 +0800 Subject: [PATCH 1/2] fix(projects): Fix the issue of tab error displaying parent localIcon Fix the issue of abnormal tab display when the parent menu has a localIcon and the child menu does not have one --- src/store/modules/tab/shared.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/store/modules/tab/shared.ts b/src/store/modules/tab/shared.ts index 702180f8..37634158 100644 --- a/src/store/modules/tab/shared.ts +++ b/src/store/modules/tab/shared.ts @@ -50,7 +50,11 @@ export function getTabIdByRoute(route: App.Global.TabRoute) { */ export function getTabByRoute(route: App.Global.TabRoute) { const { name, path, fullPath = path, meta } = route; - const { title, i18nKey, fixedIndexInTab, icon = import.meta.env.VITE_MENU_ICON, localIcon } = meta; + + const { title, i18nKey, fixedIndexInTab } = meta; + + // Get icon and localIcon from getRouteIcons function + const { icon, localIcon } = getRouteIcons(route); const label = i18nKey ? $t(i18nKey) : title; @@ -69,6 +73,29 @@ export function getTabByRoute(route: App.Global.TabRoute) { return tab; } +/** + * The vue router will automatically merge the metas of all matched items, and the icons here may be affected by other + * matching items, so they need to be processed separately + * + * @param route + */ +export function getRouteIcons(route: App.Global.TabRoute) { + // Set default value for icon at the beginning + let icon: string = import.meta.env.VITE_MENU_ICON; + let localIcon: string | undefined; + + // Route.matched only appears when there are multiple matches,so check if route.matched exists + if (route.matched) { + // Find the meta of the current route from matched + const currentRoute = route.matched.find(r => r.name === route.name); + // If icon exists in currentRoute.meta, it will overwrite the default value + icon = currentRoute?.meta?.icon || icon; + localIcon = currentRoute?.meta?.localIcon; + } + + return { icon, localIcon }; +} + /** * Get default home tab * From 2d102a054a0cadbf0d897606dde24ee78e8d547b Mon Sep 17 00:00:00 2001 From: Azir <79054161+Azir-11@users.noreply.github.com> Date: Tue, 6 Feb 2024 17:51:40 +0800 Subject: [PATCH 2/2] feat(projects): Add type to TabRoute: matched --- src/typings/app.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/typings/app.d.ts b/src/typings/app.d.ts index 5297610e..d24a609f 100644 --- a/src/typings/app.d.ts +++ b/src/typings/app.d.ts @@ -170,7 +170,7 @@ declare namespace App { }; /** Tab route */ - type TabRoute = Pick & + type TabRoute = Pick & Partial>; /** The global tab */