Vue源碼探究-組件的持久活躍
*本篇代碼位于vue/src/core/components/keep-alive.js
較新版本的Vue增加了一個內(nèi)置組件 keep-alive
邮旷,用于存儲組件狀態(tài),即便失活也能保持現(xiàn)有狀態(tài)不變,切換回來的時候不會恢復到初始狀態(tài)斗搞。由此可知翠胰,路由切換的鉤子所觸發(fā)的事件處理是無法適用于 keep-alive
組件的劣坊,那如果需要根據(jù)失活與否來給予組件事件通知战授,該怎么辦呢囤攀?如前篇所述丈氓,keep-alive
組件有兩個特有的生命周期鉤子 activated
和 deactivated
周循,用來響應(yīng)失活狀態(tài)的事件處理强法。
來看看 keep-alive
組件的實現(xiàn),代碼文件位于 components
里湾笛,目前入口文件里也只有 keep-alive
這一個內(nèi)置組件饮怯,但這個模塊的分離,會不會預示著官方將在未來開發(fā)更多具有特殊功能的內(nèi)置組件呢嚎研?
// 導入輔助函數(shù)
import { isRegExp, remove } from 'shared/util'
import { getFirstComponentChild } from 'core/vdom/helpers/index'
// 定義VNodeCache靜態(tài)類型
// 它是一個包含key名和VNode鍵值對的對象蓖墅,可想而知它是用來存儲組件的
type VNodeCache = { [key: string]: ?VNode };
// 定義getComponentName函數(shù),用于獲取組件名稱临扮,傳入組件配置對象
function getComponentName (opts: ?VNodeComponentOptions): ?string {
// 先嘗試獲取配置對象中定義的name屬性论矾,或無則獲取標簽名稱
return opts && (opts.Ctor.options.name || opts.tag)
}
// 定義matches函數(shù),進行模式匹配杆勇,傳入匹配的模式類型數(shù)據(jù)和name屬性
function matches (pattern: string | RegExp | Array<string>, name: string): boolean {
// 匹配數(shù)組模式
if (Array.isArray(pattern)) {
// 使用數(shù)組方法查找name贪壳,返回結(jié)果
return pattern.indexOf(name) > -1
} else if (typeof pattern === 'string') {
// 匹配字符串模式
// 將字符串轉(zhuǎn)換成數(shù)組查找name,返回結(jié)果
return pattern.split(',').indexOf(name) > -1
} else if (isRegExp(pattern)) {
// 匹配正則表達式
// 使用正則匹配name蚜退,返回結(jié)果
return pattern.test(name)
}
/* istanbul ignore next */
// 未匹配正確模式則返回false
return false
}
// 定義pruneCache函數(shù)闰靴,修剪keep-alive組件緩存對象
// 接受keep-alive組件實例和過濾函數(shù)
function pruneCache (keepAliveInstance: any, filter: Function) {
// 獲取組件的cache,keys钻注,_vnode屬性
const { cache, keys, _vnode } = keepAliveInstance
// 遍歷cache對象
for (const key in cache) {
// 獲取緩存資源
const cachedNode: ?VNode = cache[key]
// 如果緩存資源存在
if (cachedNode) {
// 獲取該資源的名稱
const name: ?string = getComponentName(cachedNode.componentOptions)
// 當名稱存在 且不匹配緩存過濾時
if (name && !filter(name)) {
// 執(zhí)行修剪緩存資源操作
pruneCacheEntry(cache, key, keys, _vnode)
}
}
}
}
// 定義pruneCacheEntry函數(shù)蚂且,修剪緩存條目
// 接受keep-alive實例的緩存對象和鍵名緩存對象,資源鍵名和當前資源
function pruneCacheEntry (
cache: VNodeCache,
key: string,
keys: Array<string>,
current?: VNode
) {
// 檢查緩存對象里是否已經(jīng)有以key值存儲的資源
const cached = cache[key]
// 如果有舊資源并且沒有傳入新資源參數(shù)或新舊資源標簽不同
if (cached && (!current || cached.tag !== current.tag)) {
// 銷毀該資源
cached.componentInstance.$destroy()
}
// 置空key鍵名存儲資源
cache[key] = null
// 移除key值的存儲
remove(keys, key)
}
// 定義模式匹配接收的數(shù)據(jù)類型
const patternTypes: Array<Function> = [String, RegExp, Array]
// 導出keep-alive組件實例的配置對象
export default {
// 定義組件名稱
name: 'keep-alive',
// 設(shè)置abstract屬性
abstract: true,
// 設(shè)置組件接收的屬性
props: {
// include用于包含模式匹配的資源幅恋,啟用緩存
include: patternTypes,
// exclude用于排除模式匹配的資源杏死,不啟用緩存
exclude: patternTypes,
// 最大緩存數(shù)
max: [String, Number]
},
created () {
// 實例創(chuàng)建時定義cache屬性為空對象,用于存儲資源
this.cache = Object.create(null)
// 設(shè)置keys數(shù)組捆交,用于存儲資源的key名
this.keys = []
},
destroyed () {
// 實例銷毀時一并銷毀存儲的資源并清空緩存對象
for (const key in this.cache) {
pruneCacheEntry(this.cache, key, this.keys)
}
},
mounted () {
// DOM加載完成后识埋,觀察include和exclude屬性的變動
// 回調(diào)執(zhí)行修改緩存對象的操作
this.$watch('include', val => {
pruneCache(this, name => matches(val, name))
})
this.$watch('exclude', val => {
pruneCache(this, name => !matches(val, name))
})
},
render () {
// 實例渲染函數(shù)
// 獲取keep-alive包含的子組件結(jié)構(gòu)
// keep-alive組件并不渲染任何真實DOM節(jié)點,只渲染嵌套在其中的組件資源
const slot = this.$slots.default
// 將嵌套組件dom結(jié)構(gòu)轉(zhuǎn)化成虛擬節(jié)點
const vnode: VNode = getFirstComponentChild(slot)
// 獲取嵌套組件的配置對象
const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
// 如果配置對象存在
if (componentOptions) {
// 檢查是否緩存的模式匹配
// check pattern
// 獲取嵌套組件名稱
const name: ?string = getComponentName(componentOptions)
// 獲取傳入keep-alive組件的include和exclude屬性
const { include, exclude } = this
// 如果有included零渐,且該組件不匹配included中資源
// 或者有exclude窒舟。且該組件匹配exclude中的資源
// 則返回虛擬節(jié)點,不繼續(xù)執(zhí)行緩存
if (
// not included
(include && (!name || !matches(include, name))) ||
// excluded
(exclude && name && matches(exclude, name))
) {
return vnode
}
// 獲取keep-alive組件的cache和keys對象
const { cache, keys } = this
// 獲取嵌套組件虛擬節(jié)點的key
const key: ?string = vnode.key == null
// 同樣的構(gòu)造函數(shù)可能被注冊為不同的本地組件诵盼,所以cid不是判斷的充分條件
// 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值存儲的組件資源
if (cache[key]) {
// 設(shè)置當前嵌套組件虛擬節(jié)點的componentInstance屬性
vnode.componentInstance = cache[key].componentInstance
// make current key freshest
// 從keys中移除舊key惠豺,添加新key
remove(keys, key)
keys.push(key)
} else {
// 緩存中沒有該資源,則直接存儲資源风宁,并存儲key值
cache[key] = vnode
keys.push(key)
// 如果設(shè)置了最大緩存資源數(shù)洁墙,從最開始的序號開始刪除存儲資源
// prune oldest entry
if (this.max && keys.length > parseInt(this.max)) {
pruneCacheEntry(cache, keys[0], keys, this._vnode)
}
}
// 設(shè)置該資源虛擬節(jié)點的keepAlive標識
vnode.data.keepAlive = true
}
// 返回虛擬節(jié)點或dom節(jié)點
return vnode || (slot && slot[0])
}
}
keep-alive
組件的實現(xiàn)也就這百來行代碼,分為兩部分:第一部分是定義一些處理具體實現(xiàn)的函數(shù)戒财,比如修剪緩存對象存儲資源的函數(shù)热监,匹配組件包含和過濾存儲的函數(shù);第二部分是導出一份 keep-alive
組件的應(yīng)用配置對象饮寞,仔細一下這跟我們在實際中使用的方式是一樣的孝扛,但這個組件具有已經(jīng)定義好的特殊功能
列吼,就是緩存嵌套在它之中的組件資源,實現(xiàn)持久活躍苦始。
那么實現(xiàn)原理是什么寞钥,在代碼里可以清楚得看到,這里是利用轉(zhuǎn)換組件真實DOM節(jié)點為虛擬節(jié)點將其存儲到 keep-alive
實例的 cache
對象中陌选,另外也一并存儲了資源的 key
值方便查找理郑,然后在渲染時檢測其是否符合緩存條件再進行渲染。keep-alive
的實現(xiàn)就是以上這樣簡單咨油。
最初一瞥此段代碼時您炉,不知所云。然而當開始逐步分析代碼之后役电,才發(fā)現(xiàn)原來只是沒有仔細去看邻吭,誤以為很深奧,由此可見宴霸,任何不用心的行為都不能直抵事物的本質(zhì),這是借由探索這一小部分代碼而得到的教訓膏蚓。因為在實際中有使用過這個功能瓢谢,所以體會更深,有時候難免會踩到一些坑驮瞧,看了源碼的實現(xiàn)之后氓扛,發(fā)現(xiàn)原來是自己使用方式不對,所以了解所用輪子的實現(xiàn)還是很有必要的论笔。