From c179451c4979e347ea9e92a7accf74441d02a864 Mon Sep 17 00:00:00 2001 From: zhangyh Date: Tue, 21 Oct 2025 11:28:12 +0800 Subject: [PATCH] =?UTF-8?q?doc:=20=E6=B7=BB=E5=8A=A0=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/RouteCache.vue | 7 +++---- src/router/index.ts | 9 +++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/components/RouteCache.vue b/src/components/RouteCache.vue index b5f9f8a..7821bbd 100644 --- a/src/components/RouteCache.vue +++ b/src/components/RouteCache.vue @@ -22,18 +22,17 @@ watch( const routeName = newRoute.name as string const shouldCache = newRoute.meta?.cache === true - console.log('🔄 路由变化:', { routeName, shouldCache, currentCache: cachedViews.value }) + // console.log('🔄 路由变化:', { routeName, shouldCache, currentCache: cachedViews.value }) if (shouldCache && routeName && !cachedViews.value.includes(routeName)) { - // 添加到缓存列表 cachedViews.value.push(routeName) - console.log('✅ 添加到缓存:', routeName) + // console.log('✅ 添加到缓存:', routeName) } else if (!shouldCache && routeName && cachedViews.value.includes(routeName)) { // 从缓存列表中移除 const index = cachedViews.value.indexOf(routeName) if (index > -1) { cachedViews.value.splice(index, 1) - console.log('❌ 从缓存移除:', routeName) + // console.log('❌ 从缓存移除:', routeName) } } }, diff --git a/src/router/index.ts b/src/router/index.ts index cab66c2..6c57d36 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,4 +1,13 @@ import { createRouter, createWebHistory } from 'vue-router' + +/** + * 路由缓存机制: + * 1. 设置路由的meta属性为{ cache: true },表示需要缓存 + * 2. App.vue中使用RouteCache组件,通过路由的name来进行匹配 + * 3. 路由的name默认是文件名,如果文件名与name不一致,通过defineOptions({ name: 'componentName' })来设置 + */ + + const router = createRouter({ history: createWebHistory('/'), // history: createWebHistory(import.meta.env.VITE_APP_URL),