当前位置: 首页 > news >正文

vue3 内置组件KeepAlive的使用

<KeepAlive> 是一个内置组件,它的功能是在多个组件间动态切换时缓存被移除的组件实例。

基本使用

keepAlive 通常用于包裹动态组件,以实现缓存功能。其基本语法如下:

// 父组件CompKeepAlive.vue 
<script setup>
import { shallowRef } from 'vue'
import CompA from './CompA.vue'
import CompB from './CompB.vue'const current = shallowRef(CompA)
</script><template><div class="demo"><label><input type="radio" v-model="current" :value="CompA" /> A</label><label><input type="radio" v-model="current" :value="CompB" /> B</label><KeepAlive><component :is="current"></component></KeepAlive></div>
</template>// 子组件 CompA.vue
<script setup>
import { ref } from 'vue'const count = ref(0)
</script><template><p>Current component: A</p><span>count: {{ count }}</span><button @click="count++">+</button>
</template>// 子组件 CompB.vue
<script setup>
import { ref } from 'vue'
const msg = ref('')
</script><template><p>Current component: B</p><span>Message is: {{ msg }}</span><input v-model="msg">
</template>

包裹了动态组件 。当 current 的值切换时,被缓存的组件不会被销毁,而是被保留在内存中,以便下次切换时快速渲染

包含/排除

keepAlive 提供了 include 和 exclude 属性,用于控制哪些组件需要被缓存,哪些组件需要被排除。

  • include:指定需要被缓存的组件名称。可以是一个字符串或正则表达式。
  • exclude:指定不需要被缓存的组件名称。可以是一个字符串或正则表达式
<!-- 以英文逗号分隔的字符串 -->
<KeepAlive include="a,b"><component :is="current" />
</KeepAlive><!-- 正则表达式 (需使用 `v-bind`) -->
<KeepAlive :include="/a|b/"><component :is="current" />
</KeepAlive><!-- 数组 (需使用 `v-bind`) -->
<KeepAlive :include="['a', 'b']"><component :is="current" />
</KeepAlive>

exclude组件

<keep-alive :exclude="['CompB']"><component :is="current"></component>
</keep-alive>
最大缓存实例数

keepAlive 提供了一个 max 属性,用于限制缓存实例的最大数量。当缓存实例数量超过 max 时,最早缓存的实例会被移除


<KeepAlive :max="1"><component :is="current" />
</KeepAlive>// CompA.vue 会被缓存
缓存实例的生命周期

对于缓存的组件来说,再次进入时,我们是不会执行created或者mounted等生命周期函数的:

  • 但是有时候我们确实希望监听到何时重新进入到了组件,何时离开了组件;
  • 这个时候我们可以使用activated 和 deactivated 这两个生命周期钩子函数来监听;
<script setup>
import { ref ,onActivated,onDeactivated } from 'vue'
onActivated(()=>{console.log('onActivated-- compA')})
onDeactivated(()=>{console.log('onDeactivated --compA')
})
const count = ref(0)
</script><template><p>Current component: A</p><span>count: {{ count }}</span><button @click="count++">+</button>
</template>

请注意:

  • onActivated 在组件挂载时也会调用,并且 onDeactivated 在组件卸载时也会调用。

  • 这两个钩子不仅适用于 缓存的根组件,也适用于缓存树中的后代组件。

路由组件缓存​​:需配合 vue-router 的 使用
<template>
<keep-alive :include="cachedComponents" :max="3"><router-view></router-view>
</keep-alive>
</template><script>
export default {
data() {return {cachedComponents: ['Home', 'Profile'] // 需要缓存的组件名};
}
};
</script>
http://www.xdnf.cn/news/179821.html

相关文章:

  • Spark Streaming核心编程总结(四)
  • QtDesigner中的Spacers弹簧/间隔器
  • 一主多从+自组网络,无线模拟量信号传输专治布线PTSD
  • C语言(3)—分支和循环
  • WinForm真入门(18)——DateTimePicker‌控件解析
  • 13.组合模式:思考与解读
  • MCP实战-本地MCP Server + Client实战
  • 创建一个开机自启的服务
  • 题海拾贝:P2858 [USACO06FEB] Treats for the Cows G/S
  • 大模型图像编辑那家强?
  • 多模态常见面试题
  • 新魔百和CM311-5_CH/YST/ZG代工_GK6323V100C_2+8G蓝牙版_强刷卡刷固件包(可救砖)
  • SpringMVC 前后端数据交互 中文乱码
  • 【深度剖析】贵州茅台的数字化转型(2025)(上篇)
  • 第7章 内部类与异常类
  • 【蓝桥杯省赛真题57】Scratch穿越病毒区 蓝桥杯scratch图形化编程 中小学生蓝桥杯省赛真题讲解
  • Vue.js 核心特性解析:响应式原理与组合式API实践
  • 论文检索相关网站
  • ‌RISC-V架构的低功耗MCU多电压域优化设计
  • final static 中是什么final static联合使用呢
  • 【算法刷题】
  • MySQL 8.0 忘记登录密码 mysqld --init-file重置
  • AG32 MCU系列三合一芯片,MCU+ 2K cpld + 64Mbit PSRAM,一颗芯片同时满足多种需求。
  • 清华团队提出时序聚类数据库内高效方案,已被SIGMOD 2025接收
  • 【Tools】chezmoi 跨多台不同的机器管理 dotfiles 的工具
  • 缓存并发更新的挑战
  • Python生活手册-元组:保险柜与瑞士军刀
  • 基于javaweb的SpringBoot新闻发布系统设计与实现(源码+文档+部署讲解)
  • 业务中台与数据中台:企业数字化转型的核心引擎
  • 解决qnn htp 后端不支持boolean 数据类型的方法。