亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue3?KeepAlive實(shí)現(xiàn)原理解析

 更新時(shí)間:2022年09月05日 17:04:02   作者:Axin  
KeepAlive?是一個(gè)內(nèi)置組件,那封裝一個(gè)組件對(duì)于大家來(lái)說(shuō)應(yīng)該不會(huì)有太大的困難,它的核心邏輯在于它的?render?函數(shù),它用?map?去記錄要緩存的組件,就是?[key,vnode]?的形式,這篇文章主要介紹了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)文章

最新評(píng)論