<keep-alive></keep-alive>
如果把切換出去的組件保留在內(nèi)存中,可以保留它的狀態(tài)或避免重新渲染。也不會重新請求數(shù)據(jù)他挎,為此可以添加一個 keep-alive 指令參數(shù):
include 和 exclude 屬性允許組件有條件地緩存。二者都可以用逗號分隔字符串丙唧、正則表達(dá)式或一個數(shù)組來表示:
include - 字符串或正則表達(dá)式。只有匹配的組件會被緩存蝗罗。
exclude - 字符串或正則表達(dá)式。任何匹配的組件都不會被緩存蝌戒。
<keep-alive>
<component :is="currentView">
<!-- 非活動組件將被緩存串塑! -->
</component>
</keep-alive>
--------------------------------------------------------------
實(shí)例:
<keep-alive>
<router-view></router-view>
</keep-alive>
---------------------------------------------------------------
新增:
<!-- 逗號分隔字符串 -->
<keep-alive include="a,b">
<component :is="view"></component>
</keep-alive>
<!-- 正則表達(dá)式 (使用 `v-bind`) -->
<keep-alive :include="/a|b/">
<component :is="view"></component>
</keep-alive>
<!-- 數(shù)組 (使用 `v-bind`) -->
<keep-alive :include="['a', 'b']">
<component :is="view"></component>
</keep-alive>