Pinia getter深度解析

核心内容摘要

如何高效下载微博相册:3步轻松获取高清图片
从零到认证:AI提示工程师成长全攻略

CosyVoice Android 开发实战:从零构建高效语音处理应用

Vue3缓存策略全面解析从组件级到响应式系统的优化方案Vue3提供了多层次的缓存机制主要包括组件级缓存通过KeepAlive组件实现动态组件缓存支持LRU策略和精确控制计算属性缓存自动缓存计算结果依赖不变时不会重复执行编译优化静态提升和PatchFlag标记减少不必要的DOM操作响应式系统Proxy代理缓存避免重复创建响应式对象路由缓存结合vue-router实现页面级缓存自定义缓存可扩展实现异步组件、数据请求等场景的缓存最佳实践建议根据场景选择合适的缓存策略同时注意内存管理和状态同步问题。

Vue3的缓存系统在性能与内存使用间取得了良好平衡。

Vue 3 缓存策略详解 Vue 3 缓存机制总览缓存类型技术实现作用场景API/示例组件缓存KeepAlive 组件动态组件切换KeepAlive计算属性computed 缓存依赖响应式数据computed()函数缓存记忆函数重复函数调用-响应式缓存Proxy 代理缓存减少重复代理reactive()编译优化编译时静态提升模板优化编译阶段自动虚拟DOMPatchFlag 标记DOM 更新优化渲染阶段路由缓存路由组件复用页面切换vue-router 详细缓存策略解析

KeepAlive 组件缓存核心缓存机制基本使用!-- 包裹动态组件 -- template KeepAlive component :iscurrentComponent / /KeepAlive /template !-- 包含/排除特定组件 -- KeepAlive :include[ComponentA, ComponentB] :exclude[ComponentC] :max10 component :iscurrentComponent / /KeepAlive生命周期变化// 被缓存的组件生命周期 export default { name: CachedComponent, // 正常生命周期 setup() { onMounted(() { console.log(组件挂载); }); onUnmounted(() { console.log(组件卸载 - 但被缓存时不触发); }); // 缓存特有生命周期 onActivated(() { console.log(组件激活从缓存恢复); // 恢复滚动位置、重新获取数据等 }); onDeactivated(() { console.log(组件停用进入缓存); // 暂停定时器、保存状态等 }); } };缓存策略

实现原理// 简化的缓存逻辑 class KeepAliveCache { private cache new Mapstring, VNode(); private keys: string[] []; private max: number 10; // 添加组件到缓存 add(key: string, vnode: VNode) { if (this.cache.has(key)) { // 已存在移到最近使用 this.keys this.keys.filter(k k ! key); this.keys.push(key); return; } // 检查缓存限制 if (this.keys.length this.max) { const oldestKey this.keys.shift()!; this.cache.delete(oldestKey); } this.cache.set(key, vnode); this.keys.push(key); } // 获取缓存组件 get(key: string): VNode | undefined { if (this.cache.has(key)) { // LRU最近最少使用策略 this.keys this.keys.filter(k k ! key); this.keys.push(key); return this.cache.get(key); } return undefined; } }缓存策略配置template KeepAlive :max5 // 最大缓存数LRU策略 :include/ComponentA|ComponentB/ // 正则匹配 :exclude[LoginForm] // 排除列表 // 自定义缓存 key默认基于组件名 :keycomponentKey router-view v-slot{ Component } component :isComponent :key$route.fullPath / /router-view /KeepAlive /template script setup // 动态控制缓存 import { ref } from vue; const cachedComponents ref(new Set([Dashboard, Profile])); function clearCache(componentName) { cachedComponents.value.delete(componentName); // 强制重新渲染 } /script

计算属性缓存响应式缓存自动缓存机制import { ref, computed } from vue; const user ref({ firstName: 张, lastName: 三, age: 30 }); // 计算属性自动缓存 const fullName computed(() { console.log(计算 fullName); // 依赖不变时不会重复执行 return ${user.value.firstName}${user.value.lastName}; }); const ageDescription computed(() { console.log(计算 ageDescription); return user.value.age 18 ? 成年人 : 未成年人; }); // 使用 - 多次访问不会重复计算 console.log(fullName.value); // 计算 fullName console.log(fullName.value); // 直接返回缓存 console.log(fullName.value); // 直接返回缓存 // 依赖变化时重新计算 user.value.firstName 李; console.log(fullName.value); // 重新计算 fullName缓存失效机制const list ref([1, 2, 3, 4, 5]); const filter ref(); // 带缓存的复杂计算 const filteredList computed(() { console.log(执行过滤计算); return list.value.filter(item item.toString().includes(filter.value) ); }); // 添加缓存控制 const expensiveComputation computed(() { // 依赖多个响应式数据 const result heavyCalculation(list.value, filter.value); return result; }); // 手动清除计算属性缓存通过修改依赖触发重新计算 function refreshComputation() { // 技巧临时修改依赖触发重新计算 const temp list.value; list.value []; nextTick(() { list.value temp; }); }

编译优化缓存Vue 3 新特性静态提升 (Static Hoisting)!-- 编译前 -- template div h1静态标题/h1 !-- 被提升只创建一次 -- p/p /div /template !-- 编译后生成的渲染函数 -- const _hoisted_1 /*#__PURE__*/_createElementVNode( h1, null, 静态标题, -1 /* HOISTED */ ); function render(_ctx, _cache) { return (_openBlock(), _createElementBlock(div, null, [ _hoisted_1, // 复用静态节点 _createElementVNode(p, null, _toDisplayString(_ctx.dynamicContent), 1 /* TEXT */) ])) }Patch Flag 标记缓存template !-- 编译时分析更新类型 -- div !-- 全量比较无标记 -- div classstatic/div !-- 仅文本变化标记TEXT -- div/div !-- 仅 class 变化标记CLASS -- div :classclassName内容/div !-- 仅 style 变化标记STYLE -- div :stylestyles内容/div !-- 仅 props 变化标记PROPS -- MyComponent :propAa :propBb / /div /template事件处理函数缓存template !-- 内联事件处理器会被缓存 -- button clickcount点击 /button !-- 编译优化事件缓存 -- !-- 等同于 -- button click_cache[0] || (_cache[0] $event count) 点击 /button /template script setup import { ref } from vue; const count ref(

; /script

响应式系统缓存Proxy 代理缓存// Vue 3 响应式系统内部缓存 const reactiveCache new WeakMap(); function createReactive(obj) { // 检查缓存 if (reactiveCache.has(obj)) { return reactiveCache.get(obj); } const proxy new Proxy(obj, { get(target, key, receiver) { // 依赖追踪 track(target, key); return Reflect.get(target, key, receiver); }, set(target, key, value, receiver) { const oldValue target[key]; const result Reflect.set(target, key, value, receiver); if (oldValue ! value) { // 触发更新 trigger(target, key); } return result; } }); // 缓存代理对象 reactiveCache.set(obj, proxy); return proxy; } // 使用 const original { a: 1, b: 2 }; const reactive1 reactive(original); // 创建代理 const reactive2 reactive(original); // 返回缓存的代理 console.log(reactive1 reactive

; // true

虚拟 DOM Diff 缓存基于 key 的复用策略template !-- 列表渲染中使用 key 实现 DOM 复用 -- ul li v-foritem in list :keyitem.id /li /ul !-- 条件渲染中保持组件状态 -- div v-ifshow keycomponent-a ComponentA / /div div v-else keycomponent-b ComponentB / /div /template

路由组件缓存策略vue-router 4 缓存配置// router.js import { createRouter, createWebHistory } from vue-router; const routes [ { path: /dashboard, component: () import(./views/Dashboard.vue), meta: { keepAlive: true, // 启用缓存 cacheLevel: 1, // 自定义缓存级别 cacheTimeout: 30 * 60 * 1000 // 缓存超时30分钟 } }, { path: /login, component: () import(./views/Login.vue), meta: { keepAlive: false // 不缓存登录页 } } ]; const router createRouter({ history: createWebHistory(), routes, // 滚动行为缓存 scrollBehavior(to, from, savedPosition) { if (savedPosition) { // 返回缓存的滚动位置 return savedPosition; } else { return { top: 0 }; } } });多级路由缓存!-- App.vue -- template router-view v-slot{ Component, route } KeepAlive :includecachedViews component :isComponent :keyroute.meta.usePathKey ? route.fullPath : route.name / /KeepAlive /router-view /template script setup import { computed } from vue; import { useRoute } from vue-router; const route useRoute(); // 动态管理缓存页面 const cachedViews computed(() { const views []; // 遍历路由matched收集需要缓存的组件 route.matched.forEach(record { if (record.meta.keepAlive record.components?.default) { const componentName record.components.default.name; if (componentName) { views.push(componentName); } } }); return views; }); // 清除特定路由缓存 function clearRouteCache(routeName) { // 通过修改组件key强制重新渲染 } /script

自定义缓存策略实现带缓存的异步组件// 实现带缓存的异步组件加载 const componentCache new Map(); function cachedAsyncComponent(loader, componentName) { return defineAsyncComponent({ loader: async () { // 检查缓存 if (componentCache.has(componentName)) { return componentCache.get(componentName); } const component await loader(); componentCache.set(componentName, component); // 设置缓存过期时间 setTimeout(() { componentCache.delete(componentName); }, 10 * 60 *

; // 10分钟过期 return component; }, delay: 200, // 延迟加载 timeout: 10000, // 超时时间 suspensible: true // 支持 Suspense }); } // 使用 const CachedDashboard cachedAsyncComponent( () import(./Dashboard.vue), Dashboard );数据请求缓存// 基于 Promise 的请求缓存 const requestCache new Map(); export function cachedRequest(url, options {}) { const cacheKey JSON.stringify({ url, options }); const cacheTime options.cacheTime || 5 * 60 * 1000; // 默认5分钟 // 检查缓存 const cached requestCache.get(cacheKey); if (cached Date.now() - cached.timestamp cacheTime) { return Promise.resolve(cached.data); } // 发起请求 return fetch(url, options) .then(res res.json()) .then(data { // 缓存结果 requestCache.set(cacheKey, { data, timestamp: Date.now() }); // 清理过期缓存 setTimeout(() { requestCache.delete(cacheKey); }, cacheTime); return data; }); }状态管理缓存Pinia 示例// 使用 Pinia 实现带缓存的状态管理 import { defineStore } from pinia; export const useUserStore defineStore(user, { state: () ({ userData: null, lastUpdated: null, cacheDuration: 5 * 60 * 1000 // 5分钟 }), getters: { isCacheValid: (state) { if (!state.lastUpdated) return false; return Date.now() - state.lastUpdated state.cacheDuration; } }, actions: { async fetchUser(forceRefresh false) { // 检查缓存 if (!forceRefresh this.isCacheValid this.userData) { console.log(使用缓存数据); return this.userData; } console.log(重新获取数据); const response await fetch(/api/user); this.userData await response.json(); this.lastUpdated Date.now(); return this.userData; }, clearCache() { this.userData null; this.lastUpdated null; } } });Vue 3 缓存最佳实践缓存策略选择指南场景推荐缓存策略

注意事项频繁切换的标签页KeepAlive LRU设置合理的 max 限制计算密集型操作computed 缓存确保依赖响应式数据列表渲染优化key v-memo复杂的列表使用 v-memo表单状态保持KeepAlive 或 localStorage注意数据同步图片/资源加载浏览器缓存 预加载配置合适的缓存头v-memo 指令Vue

2template !-- 手动控制组件更新 -- div v-memo[valueA, valueB] !-- 只有 valueA 或 valueB 变化时才更新 -- p/p p/p p/p /div !-- 列表优化 -- ul li v-foritem in list :keyitem.id v-memo[item.selected, item.updatedAt] !-- 仅当 selected 或 updatedAt 变化时更新 -- /li /ul /template性能监控与调试// 缓存命中率监控 const cacheStats { keepAlive: { hits: 0, misses: 0 }, computed: { hits: 0, misses: 0 }, request: { hits: 0, misses: 0 } }; // 包装计算属性进行监控 function monitoredComputed(getter) { let cache; let hasCache false; return computed(() { if (hasCache) { cacheStats.computed.hits; return cache; } else { cacheStats.computed.misses; cache getter(); hasCache true; // 依赖变化时重置缓存 nextTick(() { hasCache false; }); return cache; } }); }⚠️缓存

注意事项与问题常见缓存问题//

内存泄漏风险 const problematicCache () { // 缓存大对象或大量数据 const largeData new Array(

.fill({}); // 必须实现清除机制 }; //

状态不同步 const stateSyncIssue () { // 缓存的数据可能过期 // 需要实现过期策略或版本控制 }; //

缓存雪崩 // 大量缓存同时过期导致请求激增解决方案设置合理的缓存时间实现缓存淘汰策略LRU、LFU添加版本控制或时间戳监控缓存命中率和内存使用Vue 3 缓存

总结核心优势多层级的缓存策略- 从编译到运行时全面优化智能的自动缓存- computed、KeepAlive 自动管理精细的控制能力- include/exclude/max 等配置性能与内存平衡- LRU 策略防止内存泄漏选择建议KeepAlive用于组件切换场景computed用于依赖响应式数据的计算结果v-memo用于精确控制更新时机的复杂场景自定义缓存用于特殊业务需求Vue 3 的缓存策略是多层次、智能化的开发者可以根据具体场景选择合适的缓存方案在提升性能的同时避免常见的内存和状态管理问题。

9.1黄金网站免费入口下载-9.1黄金网站免费入口下载应用

百度百家号客服电话人工服务

123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123