Keep-alive 是 Vue 的一個內置組件,會緩存不活動的組件實例,防止重復渲染DOM。
一、原理
Vue 的緩存機制並不是直接存儲 DOM 結構,而是將 DOM 節點抽象成了一個個 VNode節點。
因此,Vue 的 keep-alive 緩存也是基於 VNode節點 而不是直接存儲 DOM 節點。
二、參數
Keep-alive 組件提供了 include 和 exclude 兩個屬性,允許組件有條件的進行緩存。
include: 字符串或正則表達式。只有匹配的組件會被緩存。
exclude: 字符串或正則表達式。任何匹配的組件都不會被緩存。
三、生命鈎子函數(如果不使用 keep-alive,生命鈎子函數不存在)
Keep-alive 組件提供了兩個生命鈎子函數,分別是 activated 和 deactivated 。
activated :當頁面存在緩存的時候執行該函數。
deactivated :在頁面結束時觸發該方法,可清除掉滾動方法等緩存。
四、組件代碼
type VNodeCache = { [key: string]: ?VNode };
const patternTypes: Array<Function> = [String, RegExp]
/* 獲取組件名稱 */
function getComponentName (opts: ?VNodeComponentOptions): ?string {
return opts && (opts.Ctor.options.name || opts.tag)
}
/* 檢測name是否匹配 */
function matches (pattern: string | RegExp, name: string): boolean {
if (typeof pattern === 'string') {
/* 字符串情況,如a,b,c */
return pattern.split(',').indexOf(name) > -1
} else if (isRegExp(pattern)) {
/* 正則 */
return pattern.test(name)
}
/* istanbul ignore next */
return false
}
/* 修正cache */
function pruneCache (cache: VNodeCache, current: VNode, filter: Function) {
for (const key in cache) {
/* 取出cache中的vnode */
const cachedNode: ?VNode = cache[key]
if (cachedNode) {
const name: ?string = getComponentName(cachedNode.componentOptions)
/* name不符合filter條件的,同時不是目前渲染的vnode時,銷毀vnode對應的組件實例(Vue實例),並從cache中移除 */
if (name && !filter(name)) {
if (cachedNode !== current) {
pruneCacheEntry(cachedNode)
}
cache[key] = null
}
}
}
}
/* 銷毀vnode對應的組件實例(Vue實例) */
function pruneCacheEntry (vnode: ?VNode) {
if (vnode) {
vnode.componentInstance.$destroy()
}
}
/* keep-alive組件 */
export default {
name: 'keep-alive',
/* 抽象組件 */
abstract: true,
props: {
include: patternTypes,
exclude: patternTypes
},
created () {
/* 緩存對象 */
this.cache = Object.create(null)
},
/* destroyed鈎子中銷毀所有cache中的組件實例 */
destroyed () {
for (const key in this.cache) {
pruneCacheEntry(this.cache[key])
}
},
watch: {
/* 監視include以及exclude,在被修改的時候對cache進行修正 */
include (val: string | RegExp) {
pruneCache(this.cache, this._vnode, name => matches(val, name))
},
exclude (val: string | RegExp) {
pruneCache(this.cache, this._vnode, name => !matches(val, name))
}
},
render () {
/* 得到slot插槽中的第一個組件 */
const vnode: VNode = getFirstComponentChild(this.$slots.default)
const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
if (componentOptions) {
// check pattern
/* 獲取組件名稱,優先獲取組件的name字段,否則是組件的tag */
const name: ?string = getComponentName(componentOptions)
/* name不在inlcude中或者在exlude中則直接返回vnode(沒有取緩存) */
if (name && (
(this.include && !matches(this.include, name)) ||
(this.exclude && matches(this.exclude, name))
)) {
return vnode
}
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
/* 如果已經做過緩存了則直接從緩存中獲取組件實例給vnode,還未緩存過則進行緩存 */
if (this.cache[key]) {
vnode.componentInstance = this.cache[key].componentInstance
} else {
this.cache[key] = vnode
}
/* keepAlive標記位 */
vnode.data.keepAlive = true
}
return vnode
}
}
將需要緩存的VNode節點保存在this.cache中,在render時,如果VNode的name符合在緩存條件(可以用include以及exclude控制),則會從this.cache中取出之前緩存的VNode實例進行渲染。
