mirror of
https://github.com/un-pany/v3-admin-vite.git
synced 2025-04-20 19:09:21 +08:00
perf: 代码优化 store/modules/tags-view
This commit is contained in:
parent
32fae72f4f
commit
836818aedd
@ -10,43 +10,34 @@ export const useTagsViewStore = defineStore("tags-view", () => {
|
||||
|
||||
//#region add
|
||||
const addVisitedView = (view: TagView) => {
|
||||
if (
|
||||
visitedViews.value.some((v, index) => {
|
||||
if (v.path === view.path) {
|
||||
if (v.fullPath !== view.fullPath) {
|
||||
// 防止 query 参数丢失
|
||||
visitedViews.value[index] = Object.assign({}, view)
|
||||
}
|
||||
return true
|
||||
}
|
||||
})
|
||||
) {
|
||||
return
|
||||
// 检查是否已经存在相同的 visitedView
|
||||
const index = visitedViews.value.findIndex((v) => v.path === view.path)
|
||||
if (index !== -1) {
|
||||
// 防止 query 参数丢失
|
||||
visitedViews.value[index].fullPath !== view.fullPath && (visitedViews.value[index] = { ...view })
|
||||
} else {
|
||||
// 添加新的 visitedView
|
||||
visitedViews.value.push({ ...view })
|
||||
}
|
||||
visitedViews.value.push(Object.assign({}, view))
|
||||
}
|
||||
|
||||
const addCachedView = (view: TagView) => {
|
||||
if (typeof view.name !== "string") return
|
||||
if (cachedViews.value.includes(view.name)) return
|
||||
if (view.meta?.keepAlive) {
|
||||
cachedViews.value.push(view.name)
|
||||
}
|
||||
if (view.meta?.keepAlive) cachedViews.value.push(view.name)
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//#region del
|
||||
const delVisitedView = (view: TagView) => {
|
||||
for (const [i, v] of visitedViews.value.entries()) {
|
||||
if (v.path === view.path) {
|
||||
visitedViews.value.splice(i, 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
const index = visitedViews.value.findIndex((v) => v.path === view.path)
|
||||
if (index !== -1) visitedViews.value.splice(index, 1)
|
||||
}
|
||||
|
||||
const delCachedView = (view: TagView) => {
|
||||
if (typeof view.name !== "string") return
|
||||
const index = cachedViews.value.indexOf(view.name)
|
||||
index > -1 && cachedViews.value.splice(index, 1)
|
||||
if (index !== -1) cachedViews.value.splice(index, 1)
|
||||
}
|
||||
//#endregion
|
||||
|
||||
@ -56,10 +47,11 @@ export const useTagsViewStore = defineStore("tags-view", () => {
|
||||
return v.meta?.affix || v.path === view.path
|
||||
})
|
||||
}
|
||||
|
||||
const delOthersCachedViews = (view: TagView) => {
|
||||
if (typeof view.name !== "string") return
|
||||
const index = cachedViews.value.indexOf(view.name)
|
||||
if (index > -1) {
|
||||
if (index !== -1) {
|
||||
cachedViews.value = cachedViews.value.slice(index, index + 1)
|
||||
} else {
|
||||
// 如果 index = -1, 没有缓存的 tags
|
||||
@ -70,10 +62,10 @@ export const useTagsViewStore = defineStore("tags-view", () => {
|
||||
|
||||
//#region delAll
|
||||
const delAllVisitedViews = () => {
|
||||
// keep affix tags
|
||||
const affixTags = visitedViews.value.filter((tag) => tag.meta?.affix)
|
||||
visitedViews.value = affixTags
|
||||
// 保留固定的 tags
|
||||
visitedViews.value = visitedViews.value.filter((tag) => tag.meta?.affix)
|
||||
}
|
||||
|
||||
const delAllCachedViews = () => {
|
||||
cachedViews.value = []
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user