Vue3?KeepAlive實(shí)現(xiàn)原理解析
思路
首先我們知道 KeepAlive 是一個(gè)內(nèi)置組件,那封裝一個(gè)組件對(duì)于大家來(lái)說(shuō)應(yīng)該不會(huì)有太大的困難,它的核心邏輯在于它的 render 函數(shù),它用 map 去記錄要緩存的組件,就是 [key,vnode] 的形式。它的核心思想就是 LRU,當(dāng)我們限制有 maxSize 的時(shí)候,超過(guò) maxSize 時(shí)我們會(huì)刪除最久沒(méi)有使用的[key, vnode],可以看看 leetcode146.LRU緩存;基本上你理清了 LRU 算法的思路,keepalive 的原理你也知道的差不多了。
代碼解析
我只貼出來(lái)能幫助大家理解的核心代碼,源碼位于core/package/runtime-core/src/components/KeepAlive.ts
setup
const cache: Cache = new Map()
const keys: Keys = new Set()
// cache sub tree after render
let pendingCacheKey: CacheKey | null = null
const cacheSubtree = () => {
// fix #1621, the pendingCacheKey could be 0
if (pendingCacheKey != null) {
cache.set(pendingCacheKey, getInnerChild(instance.subTree))
}
}
// 組件掛載和更新的時(shí)候就會(huì)緩存最新的組件
onMounted(cacheSubtree)
onUpdated(cacheSubtree)render
tips:這塊邏輯就是 setup 的返回值
const comp = vnode.type as ConcreteComponent
const key = vnode.key == null ? comp : vnode.key
pendingCacheKey = key
const cachedVNode = cache.get(key)
// tips: keys 僅僅是用于收集組件的 key 組件實(shí)際收集在cache中
if (cachedVNode) { // 節(jié)點(diǎn)存在緩存中
// copy over mounted state(復(fù)制過(guò)去的狀態(tài))
vnode.el = cachedVNode.el
vnode.component = cachedVNode.component
// 將節(jié)點(diǎn)放到隊(duì)尾
keys.delete(key)
keys.add(key)
} else { // 不存在
keys.add(key) // 加入緩存隊(duì)列
// prune oldest entry
// 超過(guò)最大值了,將最久沒(méi)有使用的組件刪除,也就是隊(duì)頭組件
if (max && keys.size > parseInt(max as string, 10)) {
pruneCacheEntry(keys.values().next().value)
}
}onActivated 和 onDeactived調(diào)用
tips:這兩塊邏輯是在 diff 中,看不懂沒(méi)關(guān)系,只要注意一個(gè)點(diǎn)就是被緩存的組件節(jié)點(diǎn)有一個(gè)標(biāo)識(shí) ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE,我們?cè)趻燧d組件的時(shí)候和卸載組件的時(shí)候判斷一下是否有這個(gè)標(biāo)識(shí),有就去調(diào)用 onActiveated 或 onDeactived;源碼位于:core/package/runtime-core/src/renderer
if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
;(parentComponent!.ctx as KeepAliveContext).deactivate(vnode)
return
}
if (
initialVNode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE ||
(parent &&
isAsyncWrapper(parent.vnode) &&
parent.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE)
) {
instance.a && queuePostRenderEffect(instance.a, parentSuspense)
if (
__COMPAT__ &&
isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
) {
queuePostRenderEffect(
() => instance.emit('hook:activated'),
parentSuspense
)
}
}到此這篇關(guān)于Vue3 KeepAlive實(shí)現(xiàn)原理的文章就介紹到這了,更多相關(guān)Vue3 KeepAlive原理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用idea創(chuàng)建第一個(gè)Vue項(xiàng)目
最近在學(xué)習(xí)vue,本文主要介紹了使用idea創(chuàng)建第一個(gè)Vue項(xiàng)目,文中根據(jù)圖文介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
使用Vue調(diào)取接口,并渲染數(shù)據(jù)的示例代碼
今天小編就為大家分享一篇使用Vue調(diào)取接口,并渲染數(shù)據(jù)的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Vue.js使用axios動(dòng)態(tài)獲取response里的data數(shù)據(jù)操作
這篇文章主要介紹了Vue.js使用axios動(dòng)態(tài)獲取response里的data數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
使用axios發(fā)送post請(qǐng)求,將JSON數(shù)據(jù)改為form類(lèi)型的示例
今天小編就為大家分享一篇使用axios發(fā)送post請(qǐng)求,將JSON數(shù)據(jù)改為form類(lèi)型的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
vue中post請(qǐng)求以a=a&b=b 的格式寫(xiě)遇到的問(wèn)題
這篇文章主要介紹了vue中post請(qǐng)求以a=a&b=b 的格式寫(xiě)遇到的問(wèn)題,需要的朋友可以參考下2018-04-04
vue post application/x-www-form-urlencoded如何實(shí)現(xiàn)傳參
這篇文章主要介紹了vue post application/x-www-form-urlencoded如何實(shí)現(xiàn)傳參問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
vuex state及mapState的基礎(chǔ)用法詳解
這篇文章主要介紹了vuex state及mapState的基礎(chǔ)用法詳解,本文通過(guò)實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-04-04

