Vue3 使用keep-alive缓存组件

keep-alive

有时候我们不希望组件被重新渲染影响使用体验;或者处于性能考虑,避免多次重复渲染降低性能。而是希望组件可以缓存下来,维持当前的状态。这时候就需要用到keep-alive组件。


<!-- 基本 -->
<keep-alive>
  <component :is="value"></component>
</keep-alive>
 
<!-- 多个条件判断的子组件 -->
<keep-alive>
  <comp-a v-if="num > 1"></comp-a>
  <comp-b v-else></comp-b>
</keep-alive>
 
<!--`` 一起使用 -->
<transition>
  <keep-alive>
    <component :is="view"></component>
  </keep-alive>
</transition>


include 和 exclude

include 和 exclude 允许组件有条件地缓存。二者都可以用逗号分隔字符串、正则表达式或一个数组来表示:


 <keep-alive :include="" :exclude=""></keep-alive>
 

max


<keep-alive :max="5">
  <component :is="value"></component>
</keep-alive>

你可能感兴趣的:(缓存,javascript,开发语言)