From dd49f8d260297abc41d739d1621a05ee8e009ad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=99=E7=A0=81=E7=94=9F=E8=8A=B1?= <18523774412@qq.com> Date: Sat, 5 Oct 2024 09:06:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=E4=BC=98=E5=8C=96=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E7=BC=93=E5=AD=98=E5=8A=9F=E8=83=BD=E7=BB=86?= =?UTF-8?q?=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/layouts/backend/router-view/main.vue | 27 ++++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/web/src/layouts/backend/router-view/main.vue b/web/src/layouts/backend/router-view/main.vue index 038615c2..3b6e284c 100644 --- a/web/src/layouts/backend/router-view/main.vue +++ b/web/src/layouts/backend/router-view/main.vue @@ -34,7 +34,7 @@ const state: { componentKey: string keepAliveComponentNameList: string[] } = reactive({ - componentKey: route.path, + componentKey: route.fullPath, keepAliveComponentNameList: [], }) @@ -48,12 +48,21 @@ const addKeepAliveComponentName = function (keepAliveName: string | undefined) { } } +const addActiveRouteKeepAlive = () => { + if (navTabs.state.activeRoute) { + const tabView = navTabs.getTabsViewDataByRoute(navTabs.state.activeRoute.fullPath, navTabs.state.tabsViewRoutes, 'normal') + if (tabView && typeof tabView.meta?.keepalive == 'string') { + addKeepAliveComponentName(tabView.meta.keepalive) + } + } +} + onBeforeMount(() => { proxy.eventBus.on('onTabViewRefresh', (menu: RouteLocationNormalized) => { state.keepAliveComponentNameList = state.keepAliveComponentNameList.filter((name: string) => menu.meta.keepalive !== name) state.componentKey = '' nextTick(() => { - state.componentKey = menu.path + state.componentKey = menu.fullPath addKeepAliveComponentName(menu.meta.keepalive as string) }) }) @@ -68,19 +77,15 @@ onUnmounted(() => { }) onMounted(() => { - // 确保刷新页面时也能正确取得当前路由 keepalive 参数 - if (typeof navTabs.state.activeRoute?.meta.keepalive == 'string') { - addKeepAliveComponentName(navTabs.state.activeRoute?.meta.keepalive) - } + // 确保刷新页面时也能正确取得当前路由 keepalive 参数(热更新) + addActiveRouteKeepAlive() }) watch( - () => route.path, + () => route.fullPath, () => { - state.componentKey = route.path - if (typeof navTabs.state.activeRoute?.meta.keepalive == 'string') { - addKeepAliveComponentName(navTabs.state.activeRoute?.meta.keepalive) - } + state.componentKey = route.fullPath + addActiveRouteKeepAlive() } )