From d2e64e618eee30a71bdbc49352518c6bd2e3d071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?piexlMax=28=E5=A5=87=E6=B7=BC?= Date: Mon, 12 Jan 2026 15:27:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=87=8D=E6=96=B0=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E7=BC=93=E5=AD=98=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/permission.js | 26 ++------------------------ web/src/pinia/modules/router.js | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/web/src/permission.js b/web/src/permission.js index 23d35a68..3de75a9e 100644 --- a/web/src/permission.js +++ b/web/src/permission.js @@ -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 } diff --git a/web/src/pinia/modules/router.js b/web/src/pinia/modules/router.js index 66850eb5..639898fc 100644 --- a/web/src/pinia/modules/router.js +++ b/web/src/pinia/modules/router.js @@ -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 } })