Vue:内置组件:KeepAlive(缓存组件实例)

一、作用

<KeepAlive></KeepAlive>能缓存包裹的所有组件,保证组件在切换时维持组件状态。

默认情况下,一个组件实例在被替换掉后会被销毁。这会导致它丢失其中所有已变化的状态——当这个组件再一次被显示时,会创建一个只带有初始状态的新实例。KeepAlive能让组件缓存,保留它的状态。

二、效果展示

组件A:

<template>
  <div class="page">
    {{ a }}
    <button @click="a++">+1</button>
  </div>
</template>
<script setup lang="ts">
import { ref } from "vue";
const a = ref(1);
</script>

组件B:

<template>
  <div class="page">这里是组件B</div>
</template>

页面,未使用KeepAlive:

<template>
  <div class="page">
    <button @click="showA = !showA">切换{{ showA }}</button>
    <component :is="showA ? componentA : componentB" />
  </div>
</template>
<script setup lang="ts">
import { ref } from "vue";
import componentA from "@/components/componentA.vue";
import componentB from "@/components/componentB.vue";
const showA = ref(true);
</script>

页面,使用KeepAlive:

<template>
  <div class="page">
    <button @click="showA = !showA">切换{{ showA }}</button>
    <KeepAlive>
      <component :is="showA ? componentA : componentB" />
    </KeepAlive>
  </div>
</template>
<script setup lang="ts">
import { ref } from "vue";
import componentA from "@/components/componentA.vue";
import componentB from "@/components/componentB.vue";
const showA = ref(true);
</script>

三、缓存组件的包含/排除

默认情况下,<KeepAlive></KeepAlive>能缓存包裹的所有组件,但我们可以通过 include 和 exclude prop 来定制该行为。这两个 prop 的值都可以是一个以英文逗号分隔的字符串、一个正则表达式,或是包含这两种类型的一个数组:

<!-- 以英文逗号分隔的字符串 -->
<KeepAlive include="a,b">
  <component :is="view" />
</KeepAlive>

<!-- 正则表达式 (需使用 `v-bind`) -->
<KeepAlive :include="/a|b/">
  <component :is="view" />
</KeepAlive>

<!-- 数组 (需使用 `v-bind`) -->
<KeepAlive :include="['a', 'b']">
  <component :is="view" />
</KeepAlive>

四、最大缓存实例数

通过max的值来限制缓存的实例数,如果缓存的实例数量即将超过指定的那个最大数量,则最久没有被访问的缓存实例将被销毁,以便为新的实例腾出空间。

<KeepAlive :max="10">
  <component :is="activeComponent" />
</KeepAlive>

五、缓存实例的生命周期

当一个组件实例从 DOM 上移除但因为被 <KeepAlive> 缓存而仍作为组件树的一部分时,它将变为不活跃状态而不是被卸载。当一个组件实例作为缓存树的一部分插入到 DOM 中时,它将重新被激活

一个持续存在的组件可以通过 onActivated() 和 onDeactivated() 注册相应的两个状态的生命周期钩子:

<script setup>
import { onActivated, onDeactivated } from 'vue'

onActivated(() => {
  // 调用时机为首次挂载
  // 以及每次从缓存中被重新插入时
})

onDeactivated(() => {
  // 在从 DOM 上移除、进入缓存
  // 以及组件卸载时调用
})
</script>

六、参考文档

Vue.js - 渐进式 JavaScript 框架 | Vue.js

相关推荐

  1. vue组件

    2024-03-14 06:00:11       31 阅读
  2. Vue-组件缓存-keep-alive

    2024-03-14 06:00:11       32 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-14 06:00:11       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-14 06:00:11       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-14 06:00:11       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-14 06:00:11       18 阅读

热门阅读

  1. 关于k8s中 storageclass 的 is-default-sc 默认存储设置

    2024-03-14 06:00:11       17 阅读
  2. k8s rancher开源平台(概念,部署示例,)

    2024-03-14 06:00:11       18 阅读
  3. k8s HPA 自动伸缩机制 (配置,资源限制,)

    2024-03-14 06:00:11       20 阅读
  4. Android 辅助功能 -抢红包

    2024-03-14 06:00:11       15 阅读
  5. C#+datax实现sql server数据同步到redis

    2024-03-14 06:00:11       16 阅读
  6. Selenium WebDriver 中用于查找网页元素的两个方法

    2024-03-14 06:00:11       19 阅读
  7. Redis 键管理和数据库管理命令详解

    2024-03-14 06:00:11       17 阅读
  8. 小程序自定义表格组件

    2024-03-14 06:00:11       17 阅读
  9. 机器学习模型—分类回归树(CART)

    2024-03-14 06:00:11       18 阅读