這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)keep-alive組件怎么在vue.js中使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),永靖企業(yè)網(wǎng)站建設(shè),永靖品牌網(wǎng)站建設(shè),網(wǎng)站定制,永靖網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,永靖網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
keep-alive是Vue.js的一個(gè)內(nèi)置組件。<keep-alive> 包裹動(dòng)態(tài)組件時(shí),會(huì)緩存不活動(dòng)的組件實(shí)例,而不是銷毀它們。它自身不會(huì)渲染一個(gè) DOM 元素,也不會(huì)出現(xiàn)在父組件鏈中。 當(dāng)組件在 <keep-alive> 內(nèi)被切換,它的 activated 和 deactivated 這兩個(gè)生命周期鉤子函數(shù)將會(huì)被對(duì)應(yīng)執(zhí)行。 它提供了include與exclude兩個(gè)屬性,允許組件有條件地進(jìn)行緩存。
舉個(gè)栗子
<keep-alive> <router-view v-if="$route.meta.keepAlive"></router-view> </keep-alive> <router-view v-if="!$route.meta.keepAlive"></router-view>
在點(diǎn)擊button時(shí)候,兩個(gè)input會(huì)發(fā)生切換,但是這時(shí)候這兩個(gè)輸入框的狀態(tài)會(huì)被緩存起來,input標(biāo)簽中的內(nèi)容不會(huì)因?yàn)榻M件的切換而消失。
* include - 字符串或正則表達(dá)式。只有匹配的組件會(huì)被緩存。
* exclude - 字符串或正則表達(dá)式。任何匹配的組件都不會(huì)被緩存。
<keep-alive include="a"> <component></component> </keep-alive>
只緩存組件別民name為a的組件
<keep-alive exclude="a"> <component></component> </keep-alive>
除了name為a的組件,其他都緩存下來
生命周期鉤子
生命鉤子 keep-alive提供了兩個(gè)生命鉤子,分別是activated與deactivated。
因?yàn)閗eep-alive會(huì)將組件保存在內(nèi)存中,并不會(huì)銷毀以及重新創(chuàng)建,所以不會(huì)重新調(diào)用組件的created等方法,需要用activated與deactivated這兩個(gè)生命鉤子來得知當(dāng)前組件是否處于活動(dòng)狀態(tài)。
深入keep-alive組件實(shí)現(xiàn)
查看vue--keep-alive組件源代碼可以得到以下信息
created鉤子會(huì)創(chuàng)建一個(gè)cache對(duì)象,用來作為緩存容器,保存vnode節(jié)點(diǎn)。
props: { include: patternTypes, exclude: patternTypes, max: [String, Number] }, created () { // 創(chuàng)建緩存對(duì)象 this.cache = Object.create(null) // 創(chuàng)建一個(gè)key別名數(shù)組(組件name) this.keys = [] },
destroyed鉤子則在組件被銷毀的時(shí)候清除cache緩存中的所有組件實(shí)例。
destroyed () { /* 遍歷銷毀所有緩存的組件實(shí)例*/ for (const key in this.cache) { pruneCacheEntry(this.cache, key, this.keys) } },
:::demo
render () { /* 獲取插槽 */ const slot = this.$slots.default /* 根據(jù)插槽獲取第一個(gè)組件組件 */ const vnode: VNode = getFirstComponentChild(slot) const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions if (componentOptions) { // 獲取組件的名稱(是否設(shè)置了組件名稱name,沒有則返回組件標(biāo)簽名稱) const name: ?string = getComponentName(componentOptions) // 解構(gòu)對(duì)象賦值常量 const { include, exclude } = this if ( /* name不在inlcude中或者在exlude中則直接返回vnode */ // not included (include && (!name || !matches(include, name))) || // excluded (exclude && name && matches(exclude, name)) ) { return vnode } const { cache, keys } = this 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 if (cache[key]) { // 判斷當(dāng)前是否有緩存,有則取緩存的實(shí)例,無則進(jìn)行緩存 vnode.componentInstance = cache[key].componentInstance // make current key freshest remove(keys, key) keys.push(key) } else { cache[key] = vnode keys.push(key) // 判斷是否設(shè)置了最大緩存實(shí)例數(shù)量,超過則刪除最老的數(shù)據(jù), if (this.max && keys.length > parseInt(this.max)) { pruneCacheEntry(cache, keys[0], keys, this._vnode) } } // 給vnode打上緩存標(biāo)記 vnode.data.keepAlive = true } return vnode || (slot && slot[0]) } // 銷毀實(shí)例 function pruneCacheEntry ( cache: VNodeCache, key: string, keys: Array<string>, current?: VNode ) { const cached = cache[key] if (cached && (!current || cached.tag !== current.tag)) { cached.componentInstance.$destroy() } cache[key] = null remove(keys, key) } // 緩存 function pruneCache (keepAliveInstance: any, filter: Function) { const { cache, keys, _vnode } = keepAliveInstance for (const key in cache) { const cachedNode: ?VNode = cache[key] if (cachedNode) { const name: ?string = getComponentName(cachedNode.componentOptions) // 組件name 不符合filler條件, 銷毀實(shí)例,移除cahe if (name && !filter(name)) { pruneCacheEntry(cache, key, keys, _vnode) } } } } // 篩選過濾函數(shù) function matches (pattern: string | RegExp | Array<string>, name: string): boolean { if (Array.isArray(pattern)) { return pattern.indexOf(name) > -1 } else if (typeof pattern === 'string') { return pattern.split(',').indexOf(name) > -1 } else if (isRegExp(pattern)) { return pattern.test(name) } /* istanbul ignore next */ return false } // 檢測(cè) include 和 exclude 數(shù)據(jù)的變化,實(shí)時(shí)寫入讀取緩存或者刪除 mounted () { this.$watch('include', val => { pruneCache(this, name => matches(val, name)) }) this.$watch('exclude', val => { pruneCache(this, name => !matches(val, name)) }) },
:::
通過查看Vue源碼可以看出,keep-alive默認(rèn)傳遞3個(gè)屬性,include 、exclude、max, max 最大可緩存的長(zhǎng)度
結(jié)合源碼我們可以實(shí)現(xiàn)一個(gè)可配置緩存的router-view
<!--exclude - 字符串或正則表達(dá)式。任何匹配的組件都不會(huì)被緩存。--> <!--TODO 匹配首先檢查組件自身的 name 選項(xiàng),如果 name 選項(xiàng)不可用,則匹配它的局部注冊(cè)名稱--> <keep-alive :exclude="keepAliveConf.value"> <router-view class="child-view" :key="$route.fullPath"></router-view> </keep-alive> <!-- 或者 --> <keep-alive :include="keepAliveConf.value"> <router-view class="child-view" :key="$route.fullPath"></router-view> </keep-alive> <!-- 具體使用 include 還是exclude 根據(jù)項(xiàng)目是否需要緩存的頁面數(shù)量多少來決定-->
創(chuàng)建一個(gè)keepAliveConf.js 放置需要匹配的組件名
// 路由組件命名集合 var arr = ['component1', 'component2']; export default {value: routeList.join()};
配置重置緩存的全局方法
import keepAliveConf from 'keepAliveConf.js' Vue.mixin({ methods: { // 傳入需要重置的組件名字 resetKeepAive(name) { const conf = keepAliveConf.value; let arr = keepAliveConf.value.split(','); if (name && typeof name === 'string') { let i = arr.indexOf(name); if (i > -1) { arr.splice(i, 1); keepAliveConf.value = arr.join(); setTimeout(() => { keepAliveConf.value = conf }, 500); } } }, } })
在合適的時(shí)機(jī)調(diào)用調(diào)用this.resetKeepAive(name),觸發(fā)keep-alive銷毀組件實(shí)例;
Vue.js內(nèi)部將DOM節(jié)點(diǎn)抽象成了一個(gè)個(gè)的VNode節(jié)點(diǎn),keep-alive組件的緩存也是基于VNode節(jié)點(diǎn)的而不是直接存儲(chǔ)DOM結(jié)構(gòu)。它將滿足條件的組件在cache對(duì)象中緩存起來,在需要重新渲染的時(shí)候再將vnode節(jié)點(diǎn)從cache對(duì)象中取出并渲染。
上述就是小編為大家分享的keep-alive組件怎么在vue.js中使用了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:keep-alive組件怎么在vue.js中使用
本文路徑:http://m.newbst.com/article20/jeeoco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)建站、品牌網(wǎng)站制作、關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)