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

Vue内置组件KeepAlive——缓存组件实例

keep-alive 是一个Vue内置组件,它的功能是在多个组件间动态切换时缓存被移除的组件实例,而不是销毁它们。

【注意】这玩意儿可不是Vue3独有的,在Vue2中就已经有了,只不过没有像Vue3那样直接显示在文档首页的侧边栏中。

使用意义

  • 性能优化:避免组件重复渲染带来的性能开销
  • 状态保持:保留组件状态(数据、滚动位置等)
  • 用户体验:实现页面的快速切换,保持用户操作状态

日常场景

  1. 对于列表页面,点击列表项跳转到详情页面;
  2. 再从详情页面返回到列表页面:
    1. 列表页面重新加载?重置了数据?
    2. 列表页面原数据加载?但页面却回滚到顶部?
    3. 列表页面保持跳转前的状态,位置不变(这个才是对用户友好的方案)

页面重新加载源于页面从销毁又到呈现的状态切换;

如果可以留存住页面状态或不让页面销毁那页面便不需要重建——使用keep-alive做页面缓存

使用方法

基本用法

<!-- 非活跃的组件将会被缓存! -->
<KeepAlive><component :is="activeComponent" />
</KeepAlive>

搭配RouterView当在处理 KeepAlive** **组件时,我们通常想要保持路由组件活跃,而不是 RouterView 本身。为了实现这个目的,我们可以将 KeepAlive 组件放置在插槽内:

<router-view v-slot="{ Component }"><keep-alive><component :is="Component" /></keep-alive>
</router-view>

包含和排除

它会根据组件的 name 选项进行匹配,所以组件如果想要条件性地被 KeepAlive 缓存,就必须显式声明一个 name 选项

在 3.2.34 或以上的版本中,使用 <script setup> 的单文件组件会自动根据文件名生成对应的 name 选项,无需再手动声明。

<!-- 以英文逗号分隔的字符串 -->
<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>

搭配路由渲染

import { createRouter, createWebHistory } from 'vue-router'
import List from '../components/list.vue'
const router = createRouter({history: createWebHistory(),routes: [{path: '/',component: () => import('../components/layout.vue'),redirect: '/home',children: [{path: '/home',component: () => import('../components/home.vue')},{path: '/list',// component: List,component: () => import('../components/lista.vue'),// 配置自定义属性keepAlive,在路由加载页面时做条件渲染meta: {keepAlive: true}},{path: '/myinfo',component: () => import('../components/myinfo.vue')},{path: '/iteminfo',component: () => import('../components/iteminfo.vue')}]}]
})
export default router
<!-- Layout.vue --><router-view v-slot="{ Component }"><component :is="Component" :key="route.path" v-if="!route.meta?.keepAlive" /><keep-alive><component :is="Component" :key="route.path" v-if="route.meta?.keepAlive" /></keep-alive>
</router-view>

缓存数量

可以通过传入 max  来限制可被缓存的最大组件实例数(组件数量)。

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

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

生命周期

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

<script setup>
import { onActivated, onDeactivated } from 'vue'onActivated(() => {// 调用时机为首次挂载// 以及每次从缓存中被重新插入时
})onDeactivated(() => {// 在从 DOM 上移除、进入缓存// 以及组件卸载时调用
})
</script>
  • onActivated 在组件挂载时也会调用,并且 onDeactivated 在组件卸载时也会调用。
  • 这两个钩子不仅适用于 <KeepAlive> 缓存的根组件,也适用于缓存树中的后代组件。

其他

举一反三:

当前的iteminfo页面路由处于layout管辖下,如若将iteminfo路由提至layout同级,怎么完成对list的缓存处理?

要先稳住layout,否则layout保不住,更别提保layout的子路由list了。

import { createRouter, createWebHistory } from 'vue-router'const router = createRouter({history: createWebHistory(import.meta.env.BASE_URL),routes: [{path:'/',component:()=>import('../components/Layout.vue'),redirect:'/home',children:[{path:'/home',component:()=>import('../components/Home.vue')},{path:'/list',component:()=>import('../components/List.vue'),meta:{keepAlive:true}},{path:'/myinfo',component:()=>import('../components/Myinfo.vue')},// {//   path:'/iteminfo',//   component: () => import('../components/ItemInfo.vue')// }]},// 挪到了外层,和layout同级{path:'/iteminfo',component: () => import('../components/ItemInfo.vue')}],
})export default router
<!-- App.vue -->
<script setup></script><template><router-view v-slot="{ Component }"><!-- 要先稳住layout,否则layout保不住,更别提保layout的子路由list了 --><KeepAlive include="Layout"><component :is="Component" /></KeepAlive></router-view>
</template><style scoped></style>

http://www.dtcms.com/a/452968.html

相关文章:

  • 品牌网站建设小h蝌蚪机械电子工程网
  • 【高并发服务器】三、正则表达式的使用
  • 网站建设好公司好深圳好的品牌策划公司
  • Java的`volatile`关键字 笔记251007
  • 【文件读写】图片木马
  • 如何避免消息丢失
  • 设备管理平台项目部署
  • 最小二乘法(Least Squares Method):原理、应用与扩展
  • 13. Pandas 透视表与交叉表分析
  • Edu161 D、E 模拟+位运算构造
  • 临床研究三千问——如何选择合适的研究类型(12)
  • 电销做网站的话术响应式网站是
  • Channel 的核心特点 (Channel vs SharedFlow 选择对比)
  • 什么网站权重高wordpress置顶代码
  • 厦门app网站设计青岛队建网站
  • 【Linux】Linux进程信号(下)
  • C++基础:(九)string类的使用与模拟实现
  • C++网络编程(二)字节序与IP地址转换
  • 从零开始XR开发:Three.js实现交互式3D积木搭建器
  • 如何解决网站只收录首页的一些办法wordpress多站点内容聚合
  • 个人备忘录的设计与实现
  • 删除cad无关线条 的ppo 随手记
  • Python AI编程在微创手术通过数据分析改善恢复的路径分析(下)
  • 深度学习之神经网络1(Neural Network)
  • pycharm下创建flask项目,配置端口问题
  • 计算机科学中的核心思想与理论
  • SpringCloud,vue3应用使用AlibabaCloudToolkit自动化部署到远程服务器上的docker
  • 如何从RSSI和SNR 判断现场的LoRaWAN的信号质量?
  • 【万字解读】品牌SEO实战指南:7步打造AI时代的搜索权威
  • 网站短期就业培训班开发公司总经理管理方案