- 從一個頁面跳轉另個頁面,再返回,第一個頁面會重新渲染,如何能不渲染呢?也就用到了keep-alive。
Vue3 用法
-
keep-alive屬性“include,exclude”的使用
注意:使用include,exclude 屬性需要給所有vue類的name賦值,否則 include,exclude將不生效
include 值為字符串或者正則表達式匹配的組件name不會被銷毀。
exclude 值為字符串或正則表達式匹配的組件name會被銷毀。
例子如下:name為playView的界面,
router.push()新界面時,它不會被unmount掉;從新界面history.back()也不會重新渲染,不會執(zhí)行setup()
<router-view v-slot="{ Component }">
<transition>
<keep-alive include="playView">
<component :is="Component" />
</keep-alive>
</transition>
</router-view>
Vue2 用法
<keep-alive>
<router-view />
</keep-alive>