doc: 添加路由缓存说明

This commit is contained in:
zhangyh
2025-10-21 11:28:12 +08:00
parent f886ba30a1
commit c179451c49
2 changed files with 12 additions and 4 deletions

View File

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

View File

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