fix: 重新实现组件缓存处理逻辑

This commit is contained in:
piexlMax(奇淼 2026-01-12 15:27:41 +08:00
parent fd2a6f45fd
commit d2e64e618e
2 changed files with 26 additions and 25 deletions

View File

@ -151,28 +151,6 @@ const removeLoading = () => {
element?.remove()
}
// 处理组件缓存
const handleKeepAlive = async (to) => {
if (!to.matched.some((item) => item.meta.keepAlive)) return
if (to.matched?.length > 2) {
for (let i = 1; i < to.matched.length; i++) {
const element = to.matched[i - 1]
if (element.name === 'layout') {
to.matched.splice(i, 1)
await handleKeepAlive(to)
continue
}
if (typeof element.components.default === 'function') {
await element.components.default()
await handleKeepAlive(to)
}
}
}
}
// 路由守卫
router.beforeEach(async (to, from) => {
@ -184,7 +162,7 @@ router.beforeEach(async (to, from) => {
// 处理元数据和缓存
to.meta.matched = [...to.matched]
await handleKeepAlive(to)
await routerStore.handleKeepAlive(to)
// 设置页面标题
document.title = getPageTitle(to.meta.title, to)
if (to.meta.client) {
@ -214,7 +192,7 @@ router.beforeEach(async (to, from) => {
// 处理异步路由
if (!routerStore.asyncRouterFlag && !WHITE_LIST.includes(from.name)) {
const setupSuccess = await setupRouter(userStore)
await setupRouter(userStore)
return to
}

View File

@ -77,6 +77,28 @@ export const useRouterStore = defineStore('router', () => {
keepAliveRouters.value = Array.from(new Set(keepArrTemp))
}
// 处理组件缓存
const handleKeepAlive = async (to) => {
if (!to.matched.some((item) => item.meta.keepAlive)) return
if (to.matched?.length > 2) {
for (let i = 1; i < to.matched.length; i++) {
const element = to.matched[i - 1]
if (element.name === 'layout') {
to.matched.splice(i, 1)
await handleKeepAlive(to)
continue
}
if (typeof element.components.default === 'function') {
await element.components.default()
await handleKeepAlive(to)
}
}
}
}
const route = useRoute()
@ -179,6 +201,7 @@ export const useRouterStore = defineStore('router', () => {
keepAliveRouters,
asyncRouterFlag,
SetAsyncRouter,
routeMap
routeMap,
handleKeepAlive
}
})