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),