vue2.0 keep-alive最佳實踐
vue2.0 keep-alive的最佳實踐,供大家參考,具體內(nèi)容如下
1.基本用法
vue2.0提供了一個keep-alive組件用來緩存組件,避免多次加載相應的組件,減少性能消耗
<keep-alive> <component> <!-- 組件將被緩存 --> </component> </keep-alive>
有時候 可能需要緩存整個站點的所有頁面,而頁面一般一進去都要觸發(fā)請求的
在使用keep-alive的情況下
<keep-alive><router-view></router-view></keep-alive>
將首次觸發(fā)請求寫在created鉤子函數(shù)中,就能實現(xiàn)緩存,
比如列表頁,去了詳情頁 回來,還是在原來的頁面
2.緩存部分頁面或者組件
(1)使用router. meta屬性
// 這是目前用的比較多的方式 <keep-alive> <router-view v-if="!$route.meta.notKeepAlive"></router-view> </keep-alive> <router-view v-if="$route.meta.notKeepAlive"></router-view>
router設(shè)置
... routes: [ { path: '/', redirect: '/index', component: Index, meta: { keepAlive: true }}, { path: '/common', component: TestParent, children: [ { path: '/test2', component: Test2, meta: { keepAlive: true } } ] } .... // 表示index和test2都使用keep-alive
(2).使用新增屬性inlcude/exclude
2.1.0后提供了include/exclude兩個屬性 可以針對性緩存相應的組件
<!-- comma-delimited string --> <keep-alive include="a,b"> <component :is="view"></component> </keep-alive> <!-- regex (use v-bind) --> <keep-alive :include="/a|b/"> <component :is="view"></component> </keep-alive> //其中a,b是組件的name
注意:這種方法都是預先知道組件的名稱的
(3)動態(tài)判斷
<keep-alive :include="includedComponents"> <router-view></router-view> </keep-alive>
includedComponents動態(tài)設(shè)置即可
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue echarts 實現(xiàn)離線中國地圖的示例代碼(細化到省份)
這篇文章主要介紹了Vue echarts 實現(xiàn)離線中國地圖,細化到省份,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09Vue使用Clipboard.JS在h5頁面中復制內(nèi)容實例詳解
在本篇文章里小編給大家整理了關(guān)于Vue使用Clipboard.JS在h5頁面中復制內(nèi)容以及相關(guān)知識點內(nèi)容,需要的朋友們可以學習下。2019-09-09Vue點擊在彈窗外部實現(xiàn)一鍵關(guān)閉的示例代碼
在Vue應用中,彈窗是一個常見的交互元素,有時我們可能希望用戶點擊彈窗外部時,彈窗能夠自動關(guān)閉,本文主要介紹了Vue點擊在彈窗外部實現(xiàn)一鍵關(guān)閉的示例代碼,感興趣的可以了解一下2024-06-06Vue+Flask實現(xiàn)簡單的登錄驗證跳轉(zhuǎn)的示例代碼
本篇文章主要介紹了Vue+Flask實現(xiàn)簡單的登錄驗證跳轉(zhuǎn)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01