vue 指定組件緩存實(shí)例詳解
keep-alive 簡(jiǎn)介
keep-alive 是 Vue 內(nèi)置的一個(gè)組件,可以使被包含的組件保留狀態(tài),或避免重新渲染。
用法也很簡(jiǎn)單:
<keep-alive> <component> <!-- 該組件將被緩存! --> </component> </keep-alive>
props
include - 字符串或正則表達(dá),只有匹配的組件會(huì)被緩存
exclude - 字符串或正則表達(dá)式,任何匹配的組件都不會(huì)被緩存
// 組件 a export default { name: 'a', data () { return {} } } <keep-alive include="a"> <component> <!-- name 為 a 的組件將被緩存! --> </component> </keep-alive>可以保留它的狀態(tài)或避免重新渲染 <keep-alive exclude="a"> <component> <!-- 除了 name 為 a 的組件都將被緩存! --> </component> </keep-alive>可以保留它的狀態(tài)或避免重新渲染 <keep-alive include="test-keep-alive"> <!-- 將緩存name為test-keep-alive的組件 --> <component></component> </keep-alive> <keep-alive include="a,b"> <!-- 將緩存name為a或者b的組件,結(jié)合動(dòng)態(tài)組件使用 --> <component :is="view"></component> </keep-alive> <!-- 使用正則表達(dá)式,需使用v-bind --> <keep-alive :include="/a|b/"> <component :is="view"></component> </keep-alive> <!-- 動(dòng)態(tài)判斷 --> <keep-alive :include="includedComponents"> <router-view></router-view> </keep-alive> <keep-alive exclude="test-keep-alive"> <!-- 將不緩存name為test-keep-alive的組件 --> <component></component> </keep-alive>
遇見 vue-router
router-view 也是一個(gè)組件,如果直接被包在 keep-alive 里面,所有路徑匹配到的視圖組件都會(huì)被緩存:
<keep-alive> <router-view> <!-- 所有路徑匹配到的視圖組件都會(huì)被緩存! --> </router-view> </keep-alive>
然而產(chǎn)品汪總是要改需求,攔都攔不住...
問題
如果只想 router-view 里面某個(gè)組件被緩存,怎么辦?
使用 include/exclude
增加 router.meta 屬性
使用 include/exclude
// 組件 a export default { name: 'a', data () { return {} } } <keep-alive include="a"> <router-view> <!-- 只有路徑匹配到的視圖 a 組件會(huì)被緩存! --> </router-view> </keep-alive>
exclude 例子類似。
缺點(diǎn):需要知道組件的 name,項(xiàng)目復(fù)雜的時(shí)候不是很好的選擇
增加 router.meta 屬性
// routes 配置 export default [ { path: '/', name: 'home', component: Home, meta: { keepAlive: true // 需要被緩存 } }, { path: '/:id', name: 'edit', component: Edit, meta: { keepAlive: false // 不需要被緩存 } } ] <keep-alive> <router-view v-if="$route.meta.keepAlive"> <!-- 這里是會(huì)被緩存的視圖組件,比如 Home! --> </router-view> </keep-alive> <router-view v-if="!$route.meta.keepAlive"> <!-- 這里是不被緩存的視圖組件,比如 Edit! --> </router-view>
優(yōu)點(diǎn):不需要例舉出需要被緩存組件名稱
【加鹽】使用 router.meta 拓展
假設(shè)這里有 3 個(gè)路由: A、B、C。
需求:
默認(rèn)顯示 A
B 跳到 A,A 不刷新
C 跳到 A,A 刷新
實(shí)現(xiàn)方式
在 A 路由里面設(shè)置 meta 屬性:
{ path: '/', name: 'A', component: A, meta: { keepAlive: true // 需要被緩存 } }
在 B 組件里面設(shè)置 beforeRouteLeave:
export default { data() { return {}; }, methods: {}, beforeRouteLeave(to, from, next) { // 設(shè)置下一個(gè)路由的 meta to.meta.keepAlive = true; // 讓 A 緩存,即不刷新 next(); } };
在 C 組件里面設(shè)置 beforeRouteLeave:
export default { data() { return {}; }, methods: {}, beforeRouteLeave(to, from, next) { // 設(shè)置下一個(gè)路由的 meta to.meta.keepAlive = false; // 讓 A 不緩存,即刷新 next(); } };
這樣便能實(shí)現(xiàn) B 回到 A,A 不刷新;而 C 回到 A 則刷新。
總結(jié)
路由大法不錯(cuò),不需要關(guān)心哪個(gè)頁面跳轉(zhuǎn)過來的,只要 router.go(-1) 就能回去,不需要額外參數(shù)。
然而在非單頁應(yīng)用的時(shí)候,keep-alive 并不能有效的緩存了= =
- vue使用keep-alive實(shí)現(xiàn)組件切換時(shí)保存原組件數(shù)據(jù)方法
- 移動(dòng)端底部導(dǎo)航固定配合vue-router實(shí)現(xiàn)組件切換功能
- 解決ios微信下vue項(xiàng)目組件切換并自動(dòng)播放音頻問題
- vue.js 動(dòng)態(tài)組件詳解
- vue使用動(dòng)態(tài)組件實(shí)現(xiàn)TAB切換效果
- vue3的動(dòng)態(tài)組件是如何工作的
- vue中keep-alive內(nèi)置組件緩存的實(shí)例代碼
- vue keep-alive 動(dòng)態(tài)刪除組件緩存的例子
- vue服務(wù)端渲染頁面緩存和組件緩存的實(shí)例詳解
- 有關(guān)vue 組件切換,動(dòng)態(tài)組件,組件緩存
相關(guān)文章
vue+element 多個(gè)相同的select不允許重復(fù)選擇問題
這篇文章主要介紹了vue+element 多個(gè)相同的select不允許重復(fù)選擇問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-07-07vue中使用極驗(yàn)驗(yàn)證碼的方法(附demo)
這篇文章主要介紹了vue中使用極驗(yàn)驗(yàn)證碼的方法(附demo)本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量
這篇文章主要給大家介紹了關(guān)于vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02關(guān)于Element-ui中Table表格無法顯示的問題及解決
這篇文章主要介紹了關(guān)于Element-ui中Table表格無法顯示的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08