引言:为什么需要路由缓存?
在现代单页应用(SPA)开发中,用户频繁在不同页面间切换是常态。每次路由切换时组件的销毁和重建不仅带来性能开销,还会导致用户状态丢失。想象一下,用户在一个复杂的表单页面填写了大量信息,切换到其他页面查看资料后返回,所有填写的内容都消失了——这种体验无疑是糟糕的。
Vue 的 <keep-alive> 组件正是为解决这类问题而生。它能够缓存不活动的组件实例,而不是销毁它们,从而保持组件状态并避免重复渲染。
keep-alive 的核心原理
组件缓存机制
<keep-alive> 是 Vue 的内置抽象组件,它自身不会渲染成 DOM 元素,也不会出现在组件的父组件链中。其核心原理基于以下几个关键点:
// keep-alive 组件的简化实现
export default {
name: 'KeepAlive',
abstract: true, // 抽象组件标记
props: {
include: [String, RegExp, Array],
exclude: [String, RegExp, Array],
max: [String, Number]
},
created() {
this.cache = Object.create(null); // 缓存对象
this.keys = []; // 缓存键列表
},
render() {
const slot = this.$slots.default;
const vnode = getFirstComponentChild(slot);
if (vnode) {
const componentOptions = vnode.componentOptions;
const name = getComponentName(componentOptions);
// 检查是否需要缓存
if (name && !this.isMatch(name)) {
return vnode;
}
const key = vnode.key == null
? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
: vnode.key;
if (this.cache[key]) {
// 从缓存中获取组件实例
vnode.componentInstance = this.cache[key].componentInstance;
// 更新 key 的位置(LRU)
remove(this.keys, key);
this.keys.push(key);
} else {
// 缓存新组件
this.cache[key] = vnode;
this.keys.push(key);
// 超出缓存上限时删除最旧的缓存
if (this.max && this.keys.length > parseInt(this.max)) {
this.pruneCacheEntry(this.keys[0]);
}
}
vnode.data.keepAlive = true;
}
return vnode;
}
};LRU 缓存策略
keep-alive 采用 LRU(Least Recently Used)缓存策略。当缓存数量达到上限时,会优先删除最久未使用的组件实例:
graph LR
A[新组件访问] --> B{缓存中存在?}
B -->|是| C[移至队尾]
B -->|否| D{缓存已满?}
D -->|是| E[删除队首]
D -->|否| F[直接添加]
E --> F
C --> G[返回缓存实例]
F --> G
生命周期钩子
被 keep-alive 缓存的组件会触发两个特殊的生命周期钩子:
export default {
name: 'CachedComponent',
activated() {
// 组件被激活时调用
// 可以在这里恢复数据监听、启动定时器等
console.log('组件被激活');
this.startPolling();
},
deactivated() {
// 组件被停用时调用
// 可以在这里清理定时器、取消数据监听等
console.log('组件被停用');
this.stopPolling();
},
methods: {
startPolling() {
this.timer = setInterval(() => {
this.fetchLatestData();
}, 5000);
},
stopPolling() {
clearInterval(this.timer);
}
}
};实际应用场景
场景一:表单数据保持
在多步骤表单或复杂表单场景中,用户可能需要在不同页面间切换查看信息:
<template>
<router-view v-slot="{ Component }">
<keep-alive :include="['FormStep1', 'FormStep2', 'FormStep3']">
<component :is="Component" />
</keep-alive>
</router-view>
</template>
<script>
export default {
name: 'FormContainer',
data() {
return {
// 表单数据可以存储在父组件或 Vuex 中
formData: {
step1: {},
step2: {},
step3: {}
}
};
}
};
</script>场景二:列表页缓存
电商或内容类应用中,用户从列表页进入详情页后返回,应保持原有的滚动位置和筛选条件:
// router/index.js
const routes = [
{
path: '/products',
name: 'ProductList',
component: () => import('@/views/ProductList.vue'),
meta: {
keepAlive: true // 标记需要缓存
}
},
{
path: '/product/:id',
name: 'ProductDetail',
component: () => import('@/views/ProductDetail.vue'),
meta: {
keepAlive: false
}
}
];
// App.vue
<template>
<router-view v-slot="{ Component, route }">
<keep-alive>
<component
:is="Component"
v-if="route.meta.keepAlive"
:key="route.name"
/>
</keep-alive>
<component
:is="Component"
v-if="!route.meta.keepAlive"
/>
</router-view>
</template>场景三:动态缓存控制
根据业务需求动态控制哪些组件需要缓存:
<template>
<div id="app">
<router-view v-slot="{ Component }">
<keep-alive :include="cachedViews">
<component :is="Component" />
</keep-alive>
</router-view>
</div>
</template>
<script>
import { mapGetters } from 'vuex';
export default {
name: 'App',
computed: {
...mapGetters(['cachedViews'])
},
watch: {
$route(to, from) {
// 根据路由变化动态管理缓存
if (to.meta.cache !== false) {
this.$store.dispatch('addCachedView', to.name);
}
// 特定条件下清除缓存
if (from.name === 'OrderComplete') {
this.$store.dispatch('removeCachedView', 'ShoppingCart');
}
}
}
};
</script>性能优化策略
1. 合理设置缓存上限
<keep-alive :max="10">
<router-view />
</keep-alive>过多的缓存会占用大量内存,建议根据应用特点设置合理的上限值。