亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue2.0 keep-alive最佳實(shí)踐

 更新時間:2017年07月06日 09:22:09   作者:spademan  
這篇文章主要為大家詳細(xì)介紹了vue2.0 keep-alive的最佳實(shí)踐,具有一定的參考價值,感興趣的小伙伴們可以參考一下

vue2.0 keep-alive的最佳實(shí)踐,供大家參考,具體內(nèi)容如下

1.基本用法

vue2.0提供了一個keep-alive組件用來緩存組件,避免多次加載相應(yīng)的組件,減少性能消耗

<keep-alive>
<component>
 <!-- 組件將被緩存 -->
</component>
</keep-alive>

有時候 可能需要緩存整個站點(diǎn)的所有頁面,而頁面一般一進(jìn)去都要觸發(fā)請求的
在使用keep-alive的情況下

<keep-alive><router-view></router-view></keep-alive>

將首次觸發(fā)請求寫在created鉤子函數(shù)中,就能實(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兩個屬性 可以針對性緩存相應(yīng)的組件

<!-- 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

注意:這種方法都是預(yù)先知道組件的名稱的

(3)動態(tài)判斷

<keep-alive :include="includedComponents">
 <router-view></router-view>
</keep-alive>

includedComponents動態(tài)設(shè)置即可

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論