Skip to content

Commit 96ad7a5

Browse files
author
yuanwj
committed
refactor(tabs): 移除冗余的newTabTitle2变量并优化标题设置逻辑
移除tabs组件中冗余的newTabTitle2变量,直接使用newTabTitle作为标题来源。同时,优化use-tabs和tabbar模块的标题设置逻辑,支持ComputedRef作为动态标题,提升代码简洁性和可维护性。
1 parent 0265c1a commit 96ad7a5

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

packages/@core/ui-kit/tabs-ui/src/components/tabs-chrome/tabs.vue

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ const tabsView = computed(() => {
4242
return props.tabs.map((tab) => {
4343
const { fullPath, meta, name, path } = tab || {};
4444
const { affixTab, icon, newTabTitle, tabClosable, title } = meta || {};
45-
const newTabTitle2 =
46-
newTabTitle instanceof Function ? newTabTitle() : newTabTitle;
4745
return {
4846
affixTab: !!affixTab,
4947
closable: Reflect.has(meta, 'tabClosable') ? !!tabClosable : true,
@@ -53,7 +51,7 @@ const tabsView = computed(() => {
5351
meta,
5452
name,
5553
path,
56-
title: (newTabTitle2 || title || name) as string,
54+
title: (newTabTitle || title || name) as string,
5755
} as TabConfig;
5856
});
5957
});

packages/@core/ui-kit/tabs-ui/src/components/tabs/tabs.vue

+1-3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ const tabsView = computed(() => {
4949
return props.tabs.map((tab) => {
5050
const { fullPath, meta, name, path } = tab || {};
5151
const { affixTab, icon, newTabTitle, tabClosable, title } = meta || {};
52-
const newTabTitle2 =
53-
newTabTitle instanceof Function ? newTabTitle() : newTabTitle;
5452
return {
5553
affixTab: !!affixTab,
5654
closable: Reflect.has(meta, 'tabClosable') ? !!tabClosable : true,
@@ -60,7 +58,7 @@ const tabsView = computed(() => {
6058
meta,
6159
name,
6260
path,
63-
title: (newTabTitle2 || title || name) as string,
61+
title: (newTabTitle || title || name) as string,
6462
} as TabConfig;
6563
});
6664
});

packages/effects/hooks/src/use-tabs.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ComputedRef } from 'vue';
12
import type { RouteLocationNormalized } from 'vue-router';
23

34
import { useRoute, useRouter } from 'vue-router';
@@ -56,20 +57,21 @@ export function useTabs() {
5657
/**
5758
* 设置当前标签页的标题
5859
*
59-
* 该函数允许设置静态字符串或动态函数作为标签标题。
60-
* 当使用函数作为标题时,每次需要显示标题时都会调用该函数获取最新的标题内容,
61-
* 这对于需要根据状态或语言变化动态更新标签标题非常有用。
60+
* @description 支持设置静态标题字符串或动态计算标题
61+
* @description 动态标题会在每次渲染时重新计算,适用于多语言或状态相关的标题
6262
*
63-
* @param {(() => string) | string} title - 要设置的标题,可以是字符串或返回字符串的函数
64-
* @example
65-
* // 设置静态标题
66-
* setTabTitle('新标签页');
63+
* @param title - 标题内容
64+
* - 静态标题: 直接传入字符串
65+
* - 动态标题: 传入 ComputedRef
6766
*
6867
* @example
69-
* // 设置动态标题(随语言变化)
70-
* setTabTitle(() => t('common.dashboard'));
68+
* // 静态标题
69+
* setTabTitle('标签页')
70+
*
71+
* // 动态标题(多语言)
72+
* setTabTitle(computed(() => t('page.title')))
7173
*/
72-
async function setTabTitle(title: (() => string) | string) {
74+
async function setTabTitle(title: ComputedRef<string> | string) {
7375
tabbarStore.setUpdateTime();
7476
await tabbarStore.setTabTitle(route, title);
7577
}

packages/stores/src/modules/tabbar.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ComputedRef } from 'vue';
12
import type { Router, RouteRecordNormalized } from 'vue-router';
23

34
import type { TabDefinition } from '@vben-core/typings';
@@ -402,22 +403,22 @@ export const useTabbarStore = defineStore('core-tabbar', {
402403
/**
403404
* @zh_CN 设置标签页标题
404405
*
405-
* 该方法允许设置静态字符串或动态函数作为标签标题。
406-
* 当使用函数作为标题时,每次需要显示标题时都会调用该函数获取最新的标题内容,
407-
* 这对于需要根据状态或语言变化动态更新标签标题非常有用。
406+
* @zh_CN 支持设置静态标题字符串或计算属性作为动态标题
407+
* @zh_CN 当标题为计算属性时,标题会随计算属性值变化而自动更新
408+
* @zh_CN 适用于需要根据状态或多语言动态更新标题的场景
408409
*
409-
* @param {TabDefinition} tab - 要设置标题的标签页
410-
* @param {(() => string) | string} title - 要设置的标题,可以是字符串或返回字符串的函数
410+
* @param {TabDefinition} tab - 标签页对象
411+
* @param {ComputedRef<string> | string} title - 标题内容,支持静态字符串或计算属性
411412
*
412413
* @example
413414
* // 设置静态标题
414415
* setTabTitle(tab, '新标签页');
415416
*
416417
* @example
417-
* // 设置动态标题(随语言变化)
418-
* setTabTitle(tab, () => t('common.dashboard'));
418+
* // 设置动态标题
419+
* setTabTitle(tab, computed(() => t('common.dashboard')));
419420
*/
420-
async setTabTitle(tab: TabDefinition, title: (() => string) | string) {
421+
async setTabTitle(tab: TabDefinition, title: ComputedRef<string> | string) {
421422
const findTab = this.tabs.find(
422423
(item) => getTabPath(item) === getTabPath(tab),
423424
);

0 commit comments

Comments
 (0)