vue2.0提供了一個(gè)keep-alive組件
用來(lái)緩存組件,避免多次加載相應(yīng)的組件,減少性能消耗
[code]
<pre><keep-alive>
<component>
</component>
</keep-alive></pre>
[/code]
有時(shí)候 可能需要緩存整個(gè)站點(diǎn)的所有頁(yè)面,而頁(yè)面一般一進(jìn)去都要觸發(fā)請(qǐng)求的
在使用keep-alive的情況下。
<keep-alive><router-view></router-view></keep-alive>
將首次觸發(fā)請(qǐng)求寫(xiě)在created鉤子函數(shù)中,就能實(shí)現(xiàn)緩存,
比如列表頁(yè),去了詳情頁(yè) 回來(lái),還是在原來(lái)的頁(yè)面
2.緩存部分頁(yè)面或者組件
(1)使用router. meta屬性
[code]
<pre>// 這是目前用的比較多的方式
<keep-alive>
<router-view v-if="$route.meta.keep_alive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keep_alive" ></router-view></pre>
[/code]
router設(shè)置
[code]
<pre>...
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</pre>
?
[/code]
歡迎參觀我的博客http://www.fefancier.com