keep-alive

Props:

  1. ​include​​ - 字符串或正则表达式。只有名称匹配的组件会被缓存。
  2. ​exclude​​ - 字符串或正则表达式。任何名称匹配的组件都不会被缓存。
  3. ​max​​ - 数字。最多可以缓存多少组件实例。

用法:

​<keep-alive>​​​ 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们。和 ​​<transition>​​​ 相似,​​<keep-alive>​​ 是一个抽象组件:它自身不会渲染一个 DOM 元素,也不会出现在组件的父组件链中。

当组件在 ​​<keep-alive>​​​ 内被切换,它的 ​​activated​​​ 和 ​​deactivated​​ 这两个生命周期钩子函数将会被对应执行。

主要用于保留组件状态或避免重新渲染。比如A页面现在滚动的位置是1/2,然后切换到B页面,再切换回来的时候页面的滚动位置还会保留。

<!-- 基本 -->
<keep-alive>
<component :is="view"></component>
</keep-alive>

<!-- 多个条件判断的子组件 -->
<keep-alive>
<comp-a v-if="a > 1"></comp-a>
<comp-b v-else></comp-b>
</keep-alive>

<!-- 和 `<transition>` 一起使用 -->
<transition>
<keep-alive>
<component :is="view"></component>
</keep-alive>
</transition>

注意,​​<keep-alive>​​​ 是用在其一个直属的子组件被开关的情形。如果你在其中有 ​​v-for​​​ 则不会工作。如果有上述的多个条件性的子元素,​​<keep-alive>​​ 要求同时只有一个子元素被渲染。

源码

keep-alive 是抽象组件,下面是源码部分的解析。

export default {
name: 'keep-alive',
abstract: true,

props: {
include: patternTypes,
exclude: patternTypes,
max: [String, Number]
},

created () {
this.cache = Object.create(null)
this.keys = []
},

destroyed () {
for (const key in this.cache) {
pruneCacheEntry(this.cache, key, this.keys)
}
},

mounted () {
// 会监控 include 和 exclude 属性,进行组件的缓存处理
this.$watch('include', val => {
pruneCache(this, name => matches(val, name))
})
this.$watch('exclude', val => {
pruneCache(this, name => !matches(val, name))
})
},

render () {
// 渲染时候
const slot = this.$slots.default // 获取插槽
const vnode: VNode = getFirstComponentChild(slot) // 默认缓存第一个组件
const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
if (componentOptions) {
// check pattern
const name: ?string = getComponentName(componentOptions)
const { include, exclude } = this
// 判断是否要缓存
if (
// not included
(include && (!name || !matches(include, name))) ||
// excluded
(exclude && name && matches(exclude, name))
) {
return vnode
}

const { cache, keys } = this
const key: ?string = vnode.key == null
// same constructor may get registered as different local components
// so cid alone is not enough (#3269)
? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
: vnode.key
// 如果组件没有 key, 表示就没有缓存,有 key 表示就缓存过了,直接获取组件的实例
if (cache[key]) {
vnode.componentInstance = cache[key].componentInstance
// make current key freshest
remove(keys, key) // 删除当前的key
keys.push(key) // 并把key放到最后面
} else {
cache[key] = vnode // 缓存vnode
keys.push(key) // 将key放到最后面
// prune oldest entry
if (this.max && keys.length > parseInt(this.max)) {
pruneCacheEntry(cache, keys[0], keys, this._vnode) // 超过了最大限制就删除第0个
}
}

vnode.data.keepAlive = true
}
return vnode || (slot && slot[0])
}
}

算法

缓存的时候会创建key值,缓存的策略中使用到的 LRU算法 规则。

谈谈你对keep-alive的了解_字符串

访问key=3这个节点的时候,需要把3移动到头部,这样能保证整个链表的头节点一定是特点数据(最近使用的数据!)

用法

​<router-view>​​​ 组件是一个 functional 组件,渲染路径匹配到的视图组件。​​<router-view>​​​ 渲染的组件还可以内嵌自己的 ​​<router-view>​​,根据嵌套路径,渲染嵌套组件。

因为它也是个组件,所以可以配合 ​​<transition>​​​ 和 ​​<keep-alive>​​​ 使用。如果两个结合一起用,要确保在内层使用 ​​<keep-alive>​​:

<transition>
<keep-alive>
<router-view></router-view>
</keep-alive>
</transition>

因为组件可能很多,所以​​<keep-alive>​​可以设置max属性,如果超出范围,那么就会有合理的删除机制,采用的就是LRU算法。