From 5ddccf6ba28453b9a35355d53d0db65f1a8876bc Mon Sep 17 00:00:00 2001 From: Netfan Date: Tue, 1 Jun 2021 01:13:19 +0800 Subject: [PATCH] feat(tabs): add setTabTitle method (#680) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加设置标签页标题的方法和演示 --- src/hooks/web/useTabs.ts | 10 ++++++++++ src/store/modules/multipleTab.ts | 11 +++++++++++ src/views/demo/feat/tabs/index.vue | 28 ++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/hooks/web/useTabs.ts b/src/hooks/web/useTabs.ts index b75cb361f..68659e5d2 100644 --- a/src/hooks/web/useTabs.ts +++ b/src/hooks/web/useTabs.ts @@ -37,6 +37,15 @@ export function useTabs(_router?: Router) { return tabStore.getTabList.find((item) => item.path === route.path)!; } + async function updateTabTitle(title: string, tab?: RouteLocationNormalized) { + const canIUse = canIUseTabs; + if (!canIUse) { + return; + } + const targetTab = tab || getCurrentTab(); + await tabStore.setTabTitle(title, targetTab); + } + async function handleTabAction(action: TableActionEnum, tab?: RouteLocationNormalized) { const canIUse = canIUseTabs; if (!canIUse) { @@ -81,5 +90,6 @@ export function useTabs(_router?: Router) { close: (tab?: RouteLocationNormalized) => { handleTabAction(TableActionEnum.CLOSE, tab); }, + setTitle: (title: string, tab?: RouteLocationNormalized) => updateTabTitle(title, tab), }; } diff --git a/src/store/modules/multipleTab.ts b/src/store/modules/multipleTab.ts index 848b7b286..08862fa63 100644 --- a/src/store/modules/multipleTab.ts +++ b/src/store/modules/multipleTab.ts @@ -286,6 +286,17 @@ export const useMultipleTabStore = defineStore({ async bulkCloseTabs(pathList: string[]) { this.tabList = this.tabList.filter((item) => !pathList.includes(item.fullPath)); }, + + /** + * Set tab's title + */ + async setTabTitle(title: string, route: RouteLocationNormalized) { + const findTab = this.getTabList.find((item) => item === route); + if (findTab) { + findTab.meta.title = title; + await this.updateCacheTab(); + } + }, }, }); diff --git a/src/views/demo/feat/tabs/index.vue b/src/views/demo/feat/tabs/index.vue index 8c50a236e..3a837c15d 100644 --- a/src/views/demo/feat/tabs/index.vue +++ b/src/views/demo/feat/tabs/index.vue @@ -1,7 +1,11 @@