From a467f501364051eb23ce6410c422d4706b8d5b35 Mon Sep 17 00:00:00 2001 From: c_7 Date: Wed, 19 Mar 2025 19:40:35 +0800 Subject: [PATCH] fix(tab): fix active tab switch issue after removal - Optimized `removeTab` to use index-based removal for better efficiency - Ensured the next tab is selected correctly when removing the active tab - Prevented unnecessary state updates" --- .../materials/src/libs/page-tab/index.vue | 2 +- src/store/modules/tab/index.ts | 23 +++++-------------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/packages/materials/src/libs/page-tab/index.vue b/packages/materials/src/libs/page-tab/index.vue index 0c0c6995..8c7b98d2 100644 --- a/packages/materials/src/libs/page-tab/index.vue +++ b/packages/materials/src/libs/page-tab/index.vue @@ -63,7 +63,7 @@ function handleClose() { diff --git a/src/store/modules/tab/index.ts b/src/store/modules/tab/index.ts index 730abe05..11804126 100644 --- a/src/store/modules/tab/index.ts +++ b/src/store/modules/tab/index.ts @@ -10,7 +10,6 @@ import { SetupStoreId } from '@/enum'; import { useThemeStore } from '../theme'; import { extractTabsByAllRoutes, - filterTabsById, filterTabsByIds, findTabByRouteName, getAllTabs, @@ -96,24 +95,14 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => { * @param tabId Tab id */ async function removeTab(tabId: string) { + const removeTabIndex = tabs.value.findIndex(tab => tab.id === tabId); + if (removeTabIndex === -1) return; + const isRemoveActiveTab = activeTabId.value === tabId; - const updatedTabs = filterTabsById(tabId, tabs.value); + const nextTab = tabs.value[removeTabIndex + 1] || homeTab.value; - function update() { - tabs.value = updatedTabs; - } - - if (!isRemoveActiveTab) { - update(); - return; - } - - const activeTab = updatedTabs.at(-1) || homeTab.value; - - if (activeTab) { - await switchRouteByTab(activeTab); - update(); - } + tabs.value.splice(removeTabIndex, 1); + if (isRemoveActiveTab && nextTab) await switchRouteByTab(nextTab); } /** remove active tab */