vue中緩存組件keep alive的介紹及使用方法
介紹
keep-alive是vue的內(nèi)置組件,可以用來緩存組件。當(dāng)它包裹動態(tài)組件時,會緩存不活動的組件實例,不會銷毀它們;將不活動的組件的狀態(tài)保留在內(nèi)存中,可以防止重復(fù)渲染DOM,減少加載事件和性能消耗。
注意:keep-alive是一個抽象組件,自身不會渲染成一個DOM元素,也不會出現(xiàn)在父組件鏈中。
原理:
在 created 函數(shù)調(diào)用時將需要緩存的 VNode 節(jié)點保存在 this.cache 中/在render(頁面渲染) 時,如果 VNode 的 name 符合緩存條件(可以用 include 以及 exclude 控制),則會從 this.cache 中取出之前緩存的 VNode 實例進行渲染。
使用
緩存所有的組件
在APP.js中緩存所有組件
<template> <div id="app"> <keep-alive> <NativeBtn> <router-view /> </keep-alive> </div> </template>
緩存某個組件
緩存某個組件就直接在該組件的外層嵌套一層<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("==== 點擊切換按鈕"); 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時,切換按鈕會有組件的創(chuàng)建和銷毀
再來看下使用keep alive的情況。修改keepAliveDemo布局代碼
<template> <div> <button @click="changeComp">切換</button> <keep-alive> <component :is="showComp" /> </keep-alive> </div> </template>
發(fā)現(xiàn)開始會有組件的創(chuàng)建,但是沒有組件的銷毀,當(dāng)兩個組件都創(chuàng)建了實例之后,再點擊切換按鈕,組件既不創(chuàng)建也不銷毀,說明使用了緩存的組件實例。
include和exclude屬性的使用
include:字符串或者正則表達式。只有匹配的組件會被緩存;
exclude:字符串或者正則表達式。任何匹配的組件都不會被緩存。
include的使用
只有匹配上的組件才會被緩存,沒匹配上的組件不會被緩存。
修改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的使用
匹配上的組件不會被被緩存,沒匹配上的組件會被緩存。
修改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 緩存的組件激活時調(diào)用
deactivated:被 keep-alive 緩存的組件失活時調(diào)用
在KeepAlivePageB和KeepAlivePageC中添加activated和deactivated鉤子,依然使用上面的exclude的例子
可以看到當(dāng)KeepAlivePageB活動使會執(zhí)行activated鉤子,失活時會調(diào)用deactivated鉤子
到此這篇關(guān)于vue中緩存組件keep alive的介紹及使用方法的文章就介紹到這了,更多相關(guān)vue keep alive內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue如何使用watch監(jiān)聽指定數(shù)據(jù)的變化
這篇文章主要介紹了vue如何使用watch監(jiān)聽指定數(shù)據(jù)的變化,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue elementUI實現(xiàn)自定義正則規(guī)則驗證
本文主要介紹了vue elementUI實現(xiàn)自定義正則規(guī)則驗證,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03基于Vue中使用節(jié)流Lodash throttle詳解
今天小編就為大家分享一篇基于Vue中使用節(jié)流Lodash throttle詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10vue+element?DatePicker實現(xiàn)日期選擇器封裝
Vue?Element?DatePicker是一款基于Vue.js的日期選擇控件,它提供了豐富的日期選擇功能,支持日期范圍選擇、日期格式化、自定義日期格式、快捷選擇等功能,極大地提高了用戶的體驗,是開發(fā)者必備的日期選擇控件。2023-02-02