vue中緩存組件keep alive的介紹及使用方法
介紹
keep-alive是vue的內(nèi)置組件,可以用來緩存組件。當(dāng)它包裹動(dòng)態(tài)組件時(shí),會(huì)緩存不活動(dòng)的組件實(shí)例,不會(huì)銷毀它們;將不活動(dòng)的組件的狀態(tài)保留在內(nèi)存中,可以防止重復(fù)渲染DOM,減少加載事件和性能消耗。
注意:keep-alive是一個(gè)抽象組件,自身不會(huì)渲染成一個(gè)DOM元素,也不會(huì)出現(xiàn)在父組件鏈中。
原理:
在 created 函數(shù)調(diào)用時(shí)將需要緩存的 VNode 節(jié)點(diǎn)保存在 this.cache 中/在render(頁面渲染) 時(shí),如果 VNode 的 name 符合緩存條件(可以用 include 以及 exclude 控制),則會(huì)從 this.cache 中取出之前緩存的 VNode 實(shí)例進(jìn)行渲染。
使用
緩存所有的組件
在APP.js中緩存所有組件
<template>
<div id="app">
<keep-alive>
<NativeBtn>
<router-view />
</keep-alive>
</div>
</template>緩存某個(gè)組件
緩存某個(gè)組件就直接在該組件的外層嵌套一層<keep-alive>
<template>
<div id="app">
<!-- 只緩存NativeBtn組件 -->
<keep-alive>
<NativeBtn />
</keep-alive>
<router-view />
</div>
</template>keep-alive的使用示例
先來看看不加keep alive的情況
代碼:
keepAliveDemo的代碼
<template>
<div>
<button @click="changeComp">切換</button>
<component :is="showComp" />
</div>
</template>
<script>
import KeepAlivePageC from "./KeepAlivePageC.vue";
import KeepAlivePageB from "./KeepAlivePageB.vue";
export default {
name: "keepAliveDemo",
components: { KeepAlivePageC, KeepAlivePageB },
data() {
return {
compType: "1",
};
},
computed: {
showComp() {
if (this.compType === "1") {
return KeepAlivePageC;
} else {
return KeepAlivePageB;
}
},
},
methods: {
changeComp() {
console.log("==== 點(diǎn)擊切換按鈕");
this.compType = this.compType === "1" ? "2" : "1";
},
},
};
</script>KeepAlivePageB的代碼
<template>
<div>KeepAlivePageB</div>
</template>
<script>
export default {
name: "KeepAlivePageB",
beforeCreate() {
console.log(" KeepAlivePageB beforeCreate 方法執(zhí)行了");
},
created() {
console.log(" KeepAlivePageB created 方法執(zhí)行了");
},
beforeMount() {
console.log(" KeepAlivePageB beforeMount 方法執(zhí)行了");
},
mounted() {
console.log(" KeepAlivePageB mounted 方法執(zhí)行了");
},
beforeUpdate() {
console.log(" KeepAlivePageB beforeUpdate 方法執(zhí)行了");
},
updated() {
console.log(" KeepAlivePageB updated 方法執(zhí)行了");
},
beforeDestroy() {
console.log(" KeepAlivePageB beforeDestroy 方法執(zhí)行了");
},
destroyed() {
console.log(" KeepAlivePageB destroyed 方法執(zhí)行了");
},
};
</script>KeepAlivePageC的代碼
<template>
<div>KeepAlivePageC</div>
</template>
<script>
export default {
name: "KeepAlivePageC",
beforeCreate() {
console.log(" KeepAlivePageC beforeCreate 方法執(zhí)行了");
},
created() {
console.log(" KeepAlivePageC created 方法執(zhí)行了");
},
beforeMount() {
console.log(" KeepAlivePageC beforeMount 方法執(zhí)行了");
},
mounted() {
console.log(" KeepAlivePageC mounted 方法執(zhí)行了");
},
beforeUpdate() {
console.log(" KeepAlivePageC beforeUpdate 方法執(zhí)行了");
},
updated() {
console.log(" KeepAlivePageC updated 方法執(zhí)行了");
},
beforeDestroy() {
console.log(" KeepAlivePageC beforeDestroy 方法執(zhí)行了");
},
destroyed() {
console.log(" KeepAlivePageC destroyed 方法執(zhí)行了");
},
};
</script>
不使用keep alive時(shí),切換按鈕會(huì)有組件的創(chuàng)建和銷毀
再來看下使用keep alive的情況。修改keepAliveDemo布局代碼
<template>
<div>
<button @click="changeComp">切換</button>
<keep-alive>
<component :is="showComp" />
</keep-alive>
</div>
</template>
發(fā)現(xiàn)開始會(huì)有組件的創(chuàng)建,但是沒有組件的銷毀,當(dāng)兩個(gè)組件都創(chuàng)建了實(shí)例之后,再點(diǎn)擊切換按鈕,組件既不創(chuàng)建也不銷毀,說明使用了緩存的組件實(shí)例。
include和exclude屬性的使用
include:字符串或者正則表達(dá)式。只有匹配的組件會(huì)被緩存;
exclude:字符串或者正則表達(dá)式。任何匹配的組件都不會(huì)被緩存。
include的使用
只有匹配上的組件才會(huì)被緩存,沒匹配上的組件不會(huì)被緩存。
修改keepAliveDemo布局代碼如下
<template>
<div>
<button @click="changeComp">切換</button>
<keep-alive include="KeepAlivePageC">
<component :is="showComp" />
</keep-alive>
</div>
</template>
可以看到KeepAlivePageC只創(chuàng)建了一次,而KeepAlivePageB一直在創(chuàng)建和銷毀
exclude的使用
匹配上的組件不會(huì)被被緩存,沒匹配上的組件會(huì)被緩存。
修改keepAliveDemo布局代碼如下
<template>
<div>
<button @click="changeComp">切換</button>
<keep-alive exclude="KeepAlivePageC">
<component :is="showComp" />
</keep-alive>
</div>
</template>
可以看到KeepAlivePageB只創(chuàng)建了一次,而KeepAlivePageC一直在創(chuàng)建和銷毀
生命周期
和keep-alive相關(guān)的生命鉤子是activated和deactivated
activated:被 keep-alive 緩存的組件激活時(shí)調(diào)用
deactivated:被 keep-alive 緩存的組件失活時(shí)調(diào)用
在KeepAlivePageB和KeepAlivePageC中添加activated和deactivated鉤子,依然使用上面的exclude的例子

可以看到當(dāng)KeepAlivePageB活動(dòng)使會(huì)執(zhí)行activated鉤子,失活時(shí)會(huì)調(diào)用deactivated鉤子
到此這篇關(guān)于vue中緩存組件keep alive的介紹及使用方法的文章就介紹到這了,更多相關(guān)vue keep alive內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue如何使用watch監(jiān)聽指定數(shù)據(jù)的變化
這篇文章主要介紹了vue如何使用watch監(jiān)聽指定數(shù)據(jù)的變化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue.js實(shí)現(xiàn)只能輸入數(shù)字的輸入框
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)只能輸入數(shù)字的輸入框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
vue elementUI實(shí)現(xiàn)自定義正則規(guī)則驗(yàn)證
本文主要介紹了vue elementUI實(shí)現(xiàn)自定義正則規(guī)則驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
基于Vue中使用節(jié)流Lodash throttle詳解
今天小編就為大家分享一篇基于Vue中使用節(jié)流Lodash throttle詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
vue項(xiàng)目配置element-ui容易遇到的坑及解決
這篇文章主要介紹了vue項(xiàng)目配置element-ui容易遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
vue+element?DatePicker實(shí)現(xiàn)日期選擇器封裝
Vue?Element?DatePicker是一款基于Vue.js的日期選擇控件,它提供了豐富的日期選擇功能,支持日期范圍選擇、日期格式化、自定義日期格式、快捷選擇等功能,極大地提高了用戶的體驗(yàn),是開發(fā)者必備的日期選擇控件。2023-02-02
詳解Vue項(xiàng)目中實(shí)現(xiàn)錨點(diǎn)定位
這篇文章主要介紹了Vue項(xiàng)目中實(shí)現(xiàn)錨點(diǎn)定位,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

