Vue?keep-alive的實現(xiàn)原理分析
keep-alive的實現(xiàn)原理
使用vue的時候,想必大家都是用過keep-alive,其作用就是緩存頁面以及其狀態(tài)。使用了這么久vue只知道如何使用但不明白其中原理,昨天翻看實現(xiàn)代碼,這里做個筆記。
這里以vue3為例
整個組件的源碼為:
const KeepAliveImpl = {
name: `KeepAlive`,
// Marker for special handling inside the renderer. We are not using a ===
// check directly on KeepAlive in the renderer, because importing it directly
// would prevent it from being tree-shaken.
__isKeepAlive: true,
props: {
include: [String, RegExp, Array],
exclude: [String, RegExp, Array],
max: [String, Number]
},
setup(props: KeepAliveProps, { slots }: SetupContext) {
const cache: Cache = new Map()
const keys: Keys = new Set()
let current: VNode | null = null
const instance = getCurrentInstance()!
// console.log('instance',instance)
// KeepAlive communicates with the instantiated renderer via the "sink"
// where the renderer passes in platform-specific functions, and the
// KeepAlive instance exposes activate/deactivate implementations.
// The whole point of this is to avoid importing KeepAlive directly in the
// renderer to facilitate tree-shaking.
const sink = instance.sink as KeepAliveSink
const {
renderer: {
move,
unmount: _unmount,
options: { createElement }
},
parentSuspense
} = sink
const storageContainer = createElement('div')
// console.log('sink',sink)
sink.activate = (vnode, container, anchor) => {
move(vnode, container, anchor, MoveType.ENTER, parentSuspense)
queuePostRenderEffect(() => {
const component = vnode.component!
component.isDeactivated = false
if (component.a !== null) {
invokeHooks(component.a)
}
}, parentSuspense)
}
sink.deactivate = (vnode: VNode) => {
move(vnode, storageContainer, null, MoveType.LEAVE, parentSuspense)
queuePostRenderEffect(() => {
const component = vnode.component!
if (component.da !== null) {
invokeHooks(component.da)
}
component.isDeactivated = true
}, parentSuspense)
}
function unmount(vnode: VNode) {
// reset the shapeFlag so it can be properly unmounted
vnode.shapeFlag = ShapeFlags.STATEFUL_COMPONENT
_unmount(vnode, instance, parentSuspense)
}
function pruneCache(filter?: (name: string) => boolean) {
cache.forEach((vnode, key) => {
const name = getName(vnode.type as Component)
if (name && (!filter || !filter(name))) {
pruneCacheEntry(key)
}
})
}
function pruneCacheEntry(key: CacheKey) {
const cached = cache.get(key) as VNode
if (!current || cached.type !== current.type) {
unmount(cached)
} else if (current) {
// current active instance should no longer be kept-alive.
// we can't unmount it now but it might be later, so reset its flag now.
current.shapeFlag = ShapeFlags.STATEFUL_COMPONENT
}
cache.delete(key)
keys.delete(key)
}
watch(
() => [props.include, props.exclude],
([include, exclude]) => {
include && pruneCache(name => matches(include, name))
exclude && pruneCache(name => matches(exclude, name))
},
{ lazy: true }
)
onBeforeUnmount(() => {
cache.forEach(unmount)
})
return () => {
if (!slots.default) {
return null
}
const children = slots.default()
let vnode = children[0]
if (children.length > 1) {
if (__DEV__) {
warn(`KeepAlive should contain exactly one component child.`)
}
current = null
return children
} else if (
!isVNode(vnode) ||
!(vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT)
) {
current = null
return vnode
}
const comp = vnode.type as Component
const name = getName(comp)
const { include, exclude, max } = props
if (
(include && (!name || !matches(include, name))) ||
(exclude && name && matches(exclude, name))
) {
return vnode
}
const key = vnode.key == null ? comp : vnode.key
const cached = cache.get(key)
// clone vnode if it's reused because we are going to mutate it
if (vnode.el) {
vnode = cloneVNode(vnode)
}
cache.set(key, vnode)
if (cached) {
// copy over mounted state
vnode.el = cached.el
vnode.anchor = cached.anchor
vnode.component = cached.component
if (vnode.transition) {
// recursively update transition hooks on subTree
setTransitionHooks(vnode, vnode.transition!)
}
// avoid vnode being mounted as fresh
vnode.shapeFlag |= ShapeFlags.COMPONENT_KEPT_ALIVE
// make this key the freshest
keys.delete(key)
keys.add(key)
} else {
keys.add(key)
// prune oldest entry
if (max && keys.size > parseInt(max as string, 10)) {
pruneCacheEntry(Array.from(keys)[0])
}
}
// avoid vnode being unmounted
vnode.shapeFlag |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
current = vnode
return vnode
}
}
}很容易看出keep-alive其實就是vue自己封裝的一個組件,和普通組件一樣。
再講keep-alive組件前先了解下vue組件的整個渲染
大致流程如下

keep-alive生命周期
組件掛載:
調(diào)用setupStatefulComponent函數(shù)觸發(fā)組件setup方法,其中組件的setup方法核心代碼其實就幾行:
return () => {
const children = slots.default()
let vnode = children[0]
cache.set(key, vnode)
if (cached) {
vnode.el = cached.el
vnode.anchor = cached.anchor
vnode.component = cached.component
vnode.shapeFlag |= ShapeFlags.COMPONENT_KEPT_ALIVE
keys.delete(key)
keys.add(key)
} else {
keys.add(key)
}
return vnode
}主要邏輯為三:
1.確認需要渲染的slot、
2.將其狀態(tài)置入緩存或讀取已存在的緩存、
3.返回slot對應(yīng)的vnode,緊接著調(diào)用setupRenderEffect,渲染出dom。
組件更新(slot變化):
當(dāng)slot變化后,首先會調(diào)用keep-alive組件的render即setup的返回函數(shù),邏輯見上面setup方法。緊接著當(dāng)某個slot卸載時,會調(diào)用deactivate函數(shù),當(dāng)某個slot重新掛載時,則會調(diào)用activate函數(shù),核心代碼如下:
const storageContainer = createElement('div')
sink.activate = (vnode, container, anchor) => {
move(vnode, container, anchor, MoveType.ENTER, parentSuspense)
queuePostRenderEffect(() => {
const component = vnode.component!
component.isDeactivated = false
if (component.a !== null) {
invokeHooks(component.a)
}
}, parentSuspense)
}
sink.deactivate = (vnode: VNode) => {
move(vnode, storageContainer, null, MoveType.LEAVE, parentSuspense)
queuePostRenderEffect(() => {
const component = vnode.component!
if (component.da !== null) {
invokeHooks(component.da)
}
component.isDeactivated = true
}, parentSuspense)
}邏輯也很簡單,當(dāng)組件卸載時,將其移入緩存的dom節(jié)點中,調(diào)用slot的deactivate生命周期,當(dāng)組件重新掛載時候,將其移入至掛載的dom節(jié)點中。
總結(jié)來說,keep-alive實現(xiàn)原理就是將對應(yīng)的狀態(tài)放入一個cache對象中,對應(yīng)的dom節(jié)點放入緩存dom中,當(dāng)下次再次需要渲染時,從對象中獲取狀態(tài),從緩存dom中移出至掛載dom節(jié)點中。
keep-alive的使用總結(jié)
在平常開發(fā)中,有些組件只需要加載一次,后面的數(shù)據(jù)將不存在變化,亦或者是組件需要緩存狀態(tài),滾動條位置等,這個時候,keep-alive的用處就立刻凸顯出來了。
1.App.vue中使用keep-alive
include表示需要緩存的頁面,exclude表示不需要緩存的頁面,你可以只設(shè)置其中一個即可,但兩個同時設(shè)置的時候,切記exclude優(yōu)先級高于include,例如a組件在exclude中和include中都存在,那么,a組件是不會被緩存的
<template> ? ? <div id="app"> ? ? ? <keep-alive :include="whiteList" :exclude="blackList"> ? ? ? ? <router-view ?v-if="isRouterAlive" ></router-view> ? ? ? </keep-alive> ? ? </div> </template>
<script>
export default {
? ? name: 'App',
? ? data(){
? ? ? return{
? ? ? ? ? isRouterAlive:true,
? ? ? ? ? whiteList:['styleLibrary','OrderList','SalesData'],
? ? ? ? ? blackList:['Footer'],
? ? ? ? ? personShow:false,
? ? ? }
? ? },
}
</script>2.App.vue中配合router進行使用
<template> ? ? <div id="app"> ? <keep-alive> ? ? <router-view v-if="$route.meta.keepAlive"></router-view> ? ?<!--緩存組件--> ? </keep-alive> ? <router-view v-if="!$route.meta.keepAlive"></router-view> ? ? ?<!--非緩存組件--> ? ? </div> </template>
將需要緩存的組件的$route.meta中的keepAlive設(shè)置為true,反之為false
?{
? ? ? path:'/login',
? ? ? name:'login',
? ? ? component:resolve=>require(['@/pages/login'],resolve),
? ? ? meta:{
? ? ? ? keepAlive:true,
? ? ? ? title:'登錄',
? ? ? ? savedPosition:true,
? ? ? }
? ? },以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue生成token保存在客戶端localStorage中的方法
本篇文章主要介紹了vue生成token保存在客戶端localStorage中的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Vue filter格式化時間戳?xí)r間成標準日期格式的方法
今天小編就為大家分享一篇Vue filter格式化時間戳?xí)r間成標準日期格式的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
electron-vite工具打包后如何通過內(nèi)置配置文件動態(tài)修改接口地址
使用electron-vite?工具開發(fā)項目打包完后每次要改接口地址都要重新打包,對于多環(huán)境切換或者頻繁變更接口地址就顯得麻煩,這篇文章主要介紹了electron-vite工具打包后通過內(nèi)置配置文件動態(tài)修改接口地址實現(xiàn)方法,需要的朋友可以參考下2024-05-05
vue實現(xiàn)圖片加載完成前的loading組件方法
下面小編就為大家分享一篇vue實現(xiàn)圖片加載完成前的loading組件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02

