提交
This commit is contained in:
1 parent
d84f4b944c
commit
7c838cfb2d
249 files changed
+50600
-18556
No files matched your search
@@ -27,6 +27,7 @@
|
||||
* - 固定标签页保护(不可关闭)
|
||||
* - KeepAlive 缓存排除管理
|
||||
* - 路由有效性验证
|
||||
* - 同时最多打开 10 个标签(超出时自动关闭最早的可关闭标签)
|
||||
* - 首页自动保留
|
||||
*
|
||||
* ## 持久化
|
||||
@@ -52,6 +53,9 @@ interface WorktabState {
|
||||
keepAliveExclude: string[]
|
||||
}
|
||||
|
||||
/** 同时打开的工作标签页数量上限(固定标签不计入淘汰,但计入总数) */
|
||||
const MAX_OPEN_TABS = 10
|
||||
|
||||
/**
|
||||
* 工作台标签页管理 Store
|
||||
*/
|
||||
@@ -167,7 +171,11 @@ export const useWorktabStore = defineStore(
|
||||
}
|
||||
|
||||
if (existingIndex === -1) {
|
||||
// 新增标签页
|
||||
// 新增标签页:超出上限时淘汰最早的可关闭标签
|
||||
if (!ensureCapacityForNewTab()) {
|
||||
return
|
||||
}
|
||||
|
||||
const insertIndex = tab.fixedTab ? findFixedTabInsertIndex() : opened.value.length
|
||||
const newTab = { ...tab }
|
||||
|
||||
@@ -400,6 +408,46 @@ export const useWorktabStore = defineStore(
|
||||
keepAliveExclude.value = keepAliveExclude.value.filter((item) => item !== name)
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭最早打开的可关闭标签(数组中最靠前的非 fixedTab)
|
||||
*/
|
||||
const evictOldestClosableTab = (): boolean => {
|
||||
const index = opened.value.findIndex(isTabClosable)
|
||||
if (index === -1) {
|
||||
return false
|
||||
}
|
||||
|
||||
const [removed] = opened.value.splice(index, 1)
|
||||
if (removed?.name) {
|
||||
addKeepAliveExclude(removed)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* 新开标签前:保证 opened.length < MAX_OPEN_TABS
|
||||
*/
|
||||
const ensureCapacityForNewTab = (): boolean => {
|
||||
while (opened.value.length >= MAX_OPEN_TABS) {
|
||||
if (!evictOldestClosableTab()) {
|
||||
console.warn(`工作标签已达上限 ${MAX_OPEN_TABS} 个,且均为固定标签,无法打开新页面`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* 持久化恢复或校验后:将数量压回上限以内
|
||||
*/
|
||||
const trimTabsToMaxOpen = (): void => {
|
||||
while (opened.value.length > MAX_OPEN_TABS) {
|
||||
if (!evictOldestClosableTab()) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将传入的一组选项卡的组件名称标记为排除缓存
|
||||
*/
|
||||
@@ -480,6 +528,8 @@ export const useWorktabStore = defineStore(
|
||||
opened.value = validTabs
|
||||
}
|
||||
|
||||
trimTabsToMaxOpen()
|
||||
|
||||
// 验证当前激活标签的有效性
|
||||
const isCurrentValid =
|
||||
current.value && !shouldIgnoreTab(current.value) && isTabRouteValid(current.value)
|
||||
|
||||
Reference in new issue
Block a user