详细分析KeepAlive的基本知识 并缓存路由(附Demo)

目录

  • 前言
  • 1. 基本知识
  • 2. Demo
    • 2.1 基本
    • 2.2 拓展
    • 2.3 终极
  • 3. 实战

前言

🤟 找工作,来万码优才:👉 #小程序://万码优才/r6rqmzDaXpYkJZF

基本知识推荐阅读:KeepAlive知识点

从实战中学习,源自实战中vue路由的缓存设置

<router-view v-if="routerAlive"><template #default="{ Component, route }"><keep-alive :include="getCaches"><component :is="Component" :key="route.fullPath" /></keep-alive></template>
</router-view>

截图如下:

在这里插入图片描述

1. 基本知识

<KeepAlive> 内置组件,用于缓存动态组件或路由组件,以提高性能
可以保留组件的状态,避免重复渲染和生命周期钩子的重新调用

KeepAlive 作用

  • 缓存组件,提高性能,避免组件反复销毁和创建
  • 保留组件的状态,例如表单填写内容、滚动位置等
  • 控制缓存,可以指定哪些组件需要被缓存,哪些不需要

KeepAlive 适用场景

  • 需要缓存的 多页面表单
  • 列表详情页切换 时,保留列表的滚动位置
  • 复杂组件切换时,避免重新渲染带来的性能开销
功能说明
KeepAlive用于缓存组件,提高性能
include指定要缓存的组件(支持字符串或数组)
exclude指定不缓存的组件
max限制最大缓存组件数量
activated()组件被激活时触发
deactivated()组件被缓存时触发

include 和 exclude
可以通过 include 和 exclude 来决定哪些组件需要被缓存,哪些不需要

<keep-alive include="ComponentA"><component :is="currentComponent"></component>
</keep-alive>

只有 ComponentA 会被缓存,而 ComponentB 不会

或者:

<keep-alive :include="['ComponentA', 'ComponentC']"><component :is="currentComponent"></component>
</keep-alive>

只有 ComponentA 和 ComponentC 会被缓存
如果需要排除某个组件:

<keep-alive exclude="ComponentB"><component :is="currentComponent"></component>
</keep-alive>

ComponentB 不会被缓存,而其他组件都会被缓存

max —— 设置缓存组件的最大数量
如果缓存的组件较多,可以设置 max 限制最多缓存多少个组件
只缓存最近的两个组件,超出的组件会被销毁

<keep-alive :max="2"><component :is="currentComponent"></component>
</keep-alive>

KeepAlive 生命周期钩子
Vue 提供了两个专门用于 KeepAlive 组件的生命周期钩子:
activated():组件被激活(切换到缓存中的组件时调用)
deactivated():组件被缓存(切换到另一个组件时调用)

<script>
export default {created() {console.log("组件创建");},activated() {console.log("组件被激活");},deactivated() {console.log("组件被缓存");},destroyed() {console.log("组件被销毁");},
};
</script>

运行效果:

  1. 组件首次渲染时,created() 会触发
  2. 当组件切换回来时,activated() 会触发,而不会重新执行 created()
  3. 切换到别的组件时,deactivated() 触发,而不会执行 destroyed()
  4. 只有当缓存被清除时,才会执行 destroyed()

2. Demo

2.1 基本

动态组件缓存

<template><div><button @click="currentComponent = 'ComponentA'">切换到A</button><button @click="currentComponent = 'ComponentB'">切换到B</button><keep-alive><component :is="currentComponent"></component></keep-alive></div>
</template><script>
import ComponentA from "./ComponentA.vue";
import ComponentB from "./ComponentB.vue";export default {data() {return {currentComponent: "ComponentA",};},components: {ComponentA,ComponentB,},
};
</script>

组件 A (ComponentA.vue):

<template><div><h3>组件 A</h3><input v-model="text" placeholder="输入内容会被缓存" /></div>
</template><script>
export default {data() {return {text: "",};},created() {console.log("ComponentA 创建");},destroyed() {console.log("ComponentA 被销毁");},
};
</script>

组件 B (ComponentB.vue):

<template><div><h3>组件 B</h3></div>
</template><script>
export default {created() {console.log("ComponentB 创建");},destroyed() {console.log("ComponentB 被销毁");},
};
</script>

一个最明显的变化就是:
在 ComponentA 中输入一些文字,然后切换到 ComponentB,再切回来,发现输入内容还在(ComponentA 没有被销毁)

2.2 拓展

KeepAlive 也可以与 Vue Router 结合,缓存路由组件
这样在 PageA 和 PageB 之间切换时,PageA 不会被销毁,而是会被缓存

<template><div><router-link to="/pageA">页面 A</router-link><router-link to="/pageB">页面 B</router-link><keep-alive><router-view></router-view></keep-alive></div>
</template>

路由配置:

import { createRouter, createWebHistory } from "vue-router";
import PageA from "./PageA.vue";
import PageB from "./PageB.vue";const routes = [{ path: "/pageA", component: PageA },{ path: "/pageB", component: PageB },
];const router = createRouter({history: createWebHistory(),routes,
});export default router;

2.3 终极

这也是实战中常用的一种方式,从实战中抽离出基本的Demo,以 Vue 3 + Vue Router 4 + Pinia

装依赖:npm install vue-router@4 pinia

main.ts(应用入口)

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import { createRouter, createWebHistory } from 'vue-router'
import App from './App.vue'
import Home from './views/Home.vue'
import About from './views/About.vue'
import Profile from './views/Profile.vue'const pinia = createPinia()// 定义路由
const routes = [{ path: '/', component: Home, name: 'Home' },{ path: '/about', component: About, name: 'About' },{ path: '/profile', component: Profile, name: 'Profile' }
]const router = createRouter({history: createWebHistory(),routes
})const app = createApp(App)
app.use(pinia)
app.use(router)
app.mount('#app')

store/tagsView.ts(Pinia 状态管理 - 维护缓存组件列表)

import { defineStore } from 'pinia'export const useTagsViewStore = defineStore('tagsView', {state: () => ({cachedViews: new Set<string>()}),getters: {getCachedViews(): string[] {return Array.from(this.cachedViews)}},actions: {addCachedView(name: string) {this.cachedViews.add(name)},removeCachedView(name: string) {this.cachedViews.delete(name)},clearCachedViews() {this.cachedViews.clear()}}
})

App.vue(KeepAlive 组件封装)

<script setup lang="ts">
import { computed, ref, provide, nextTick } from 'vue'
import { useTagsViewStore } from './store/tagsView'
import { RouterView } from 'vue-router'const tagsViewStore = useTagsViewStore()
const getCaches = computed(() => tagsViewStore.getCachedViews)// 无感刷新功能
const routerAlive = ref(true)
const reload = () => {routerAlive.value = falsenextTick(() => (routerAlive.value = true))
}
provide('reload', reload)
</script><template><div><router-view v-if="routerAlive"><template #default="{ Component, route }"><keep-alive :include="getCaches"><component :is="Component" :key="route.fullPath" /></keep-alive></template></router-view></div>
</template>

views/Home.vue

<script setup lang="ts">
import { inject } from 'vue'const reload = inject('reload') as () => void
</script><template><div><h1>Home Page</h1><button @click="reload">刷新当前组件</button></div>
</template>

views/About.vue

<template><div><h1>About Page</h1></div>
</template>

views/Profile.vue

<template><div><h1>Profile Page</h1></div>
</template>

动态路由的常用操作

  1. 动态添加路由
    在 router.ts 中可以动态添加路由:
import router from './router'const newRoute = {path: '/new-page',component: () => import('@/views/NewPage.vue'),name: 'NewPage'
}router.addRoute(newRoute)
  1. 动态移除路由:router.removeRoute('NewPage')

  2. 监听路由变化

import { useRoute, useRouter } from 'vue-router'
import { watch } from 'vue'const route = useRoute()
const router = useRouter()watch(() => route.fullPath, (newPath) => {console.log('路由发生变化:', newPath)
})

3. 实战

以ruoyi-vue-pro实战的Demo进行讲解,源码:芋道源码/ruoyi-vue-pro

具体KeepAlive,其文件在App.vue中

<router-view v-if="routerAlive"><template #default="{ Component, route }"><keep-alive :include="getCaches"><component :is="Component" :key="route.fullPath" /></keep-alive></template>
</router-view>

通过组件的设置是否路由进行缓存,后续获取到这个路由信息时,对应判定是否该路由有缓存信息

const getCaches = computed((): string[] => {const caches = tagsViewStore.getCachedViewsconsole.log('当前缓存的组件:', caches) // 打印缓存的组件名称return caches
})const tagsView = computed(() => appStore.getTagsView)//region 无感刷新
const routerAlive = ref(true)
// 无感刷新,防止出现页面闪烁白屏
const reload = () => {routerAlive.value = falsenextTick(() => (routerAlive.value = true))
}
// 为组件后代提供刷新方法
provide('reload', reload)

对应的tagsView信息如下:
在这里插入图片描述

后续在tsx中进行引用
在这里插入图片描述

后续新增路由的时候,其路由地址 要和 defineOptions({ name: 'xxx' }) 对应一致

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/27884.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AI编程,常见的AI编程工具有哪些?如何用AI编程做一个简单的小软件?

随着AI的快速发展&#xff0c;编程不再是专业程序员的专属技能&#xff0c;而逐渐成为一种普通人也能掌握的工具。 如今&#xff0c;即使没有编程基础&#xff0c;也可以通过几种方式轻松入门AI编程&#xff0c;包括直接使用大语言模型进行编程、借助特定的AI软件进行可视化编程…

探秘 Linux 系统编程:进程地址空间的奇妙世界

亲爱的读者朋友们&#x1f603;&#xff0c;此文开启知识盛宴与思想碰撞&#x1f389;。 快来参与讨论&#x1f4ac;&#xff0c;点赞&#x1f44d;、收藏⭐、分享&#x1f4e4;&#xff0c;共创活力社区。 在 Linux 系统编程的领域里&#xff0c;进程地址空间可是个相当重要的…

2025-03-04 学习记录--C/C++-PTA 习题5-5 使用函数统计指定数字的个数

合抱之木&#xff0c;生于毫末&#xff1b;九层之台&#xff0c;起于累土&#xff1b;千里之行&#xff0c;始于足下。&#x1f4aa;&#x1f3fb; 一、题目描述 ⭐️ 二、代码&#xff08;C语言&#xff09;⭐️ #include <stdio.h>int CountDigit( int number, int di…

25年第四本【认知觉醒】

《认知觉醒》&#xff1a;一场与大脑的深度谈判 在信息爆炸的焦虑时代&#xff0c;我们像被抛入湍流的溺水者&#xff0c;拼命抓取各种自我提升的浮木&#xff0c;却在知识的漩涡中越陷越深。这不是一本简单的成功学指南&#xff0c;而是一场关于人类认知系统的深度对话&#…

汽车视频智能包装创作解决方案,让旅途记忆一键升级为影视级大片

在智能汽车时代&#xff0c;行车记录已不再是简单的影像留存&#xff0c;而是承载情感与创意的载体。美摄科技依托20余年视音频领域技术积累&#xff0c;推出汽车视频智能包装创作解决方案&#xff0c;以AI驱动影像处理与艺术创作&#xff0c;重新定义车载视频体验&#xff0c;…

DeepSeek 智慧城市应用:交通流量预测(918)

**摘要&#xff1a;**本文探讨了利用 DeepSeek 技术框架解决城市交通流量预测问题的方法&#xff0c;主要内容包括基于时空图卷积网络&#xff08;ST - GCN&#xff09;的预测模型、多传感器数据融合策略以及实时推理 API 服务的搭建&#xff0c;旨在为智慧城市的交通管理提供高…

如何在随机振动分析中包括缓冲器

总结 在随机振动分析中&#xff0c;准确模拟系统的动态行为对于预测其在随机激励下的响应至关重要。在这种情况下&#xff0c;分立阻尼器&#xff08;如减振器&#xff09;是必不可少的组件&#xff0c;因为它有助于模拟实际系统中的能量耗散机制。通过将离散阻尼器集成到模型…

python3.13安装教程【2025】python3.13超详细图文教程(包含安装包)

文章目录 前言一、python3.13安装包下载二、Python 3.13安装步骤三、Python3.13验证 前言 本教程将为你详细介绍 Python 3.13 python3.13安装教程&#xff0c;帮助你顺利搭建起 Python 3.13 开发环境&#xff0c;快速投身于 Python 编程的精彩实践中。 一、python3.13安装包下…

Transformer 代码剖析6 - 位置编码 (pytorch实现)

一、位置编码的数学原理与设计思想 1.1 核心公式解析 位置编码采用正弦余弦交替编码方案&#xff1a; P E ( p o s , 2 i ) sin ⁡ ( p o s 1000 0 2 i / d m o d e l ) P E ( p o s , 2 i 1 ) cos ⁡ ( p o s 1000 0 2 i / d m o d e l ) PE_{(pos,2i)} \sin\left(\fra…

CF 452A.Eevee(Java实现)

题目分析 输入一个数字-长度&#xff0c;输入一个字符串。判断这个字符串是具体的哪一个单词 思路分析 首先给了长度&#xff0c;那我先判断长度相同的单词&#xff0c;然后再一一对比&#xff0c;如果都能通过&#xff0c;那就输出这个单词 代码 import java.util.*;public …

【监控】使用Prometheus+Grafana搭建服务器运维监控面板(含带BearerToken的Exporter配置)

【监控】使用PrometheusGrafana搭建服务器运维监控面板&#xff08;含带BearerToken的Exporter配置&#xff09; 文章目录 1、Grafana 数据可视化面板2、Prometheus - 收集和存储指标数据3、Exporter - 采集和上报指标数据 1、Grafana 数据可视化面板 Grafana 是一个开源的可视…

ADC采集模块与MCU内置ADC性能对比

2.5V基准电压源&#xff1a; 1. 精度更高&#xff0c;误差更小 ADR03B 具有 0.1% 或更小的初始精度&#xff0c;而 电阻分压方式的误差主要来自电阻的容差&#xff08;通常 1% 或 0.5%&#xff09;。长期稳定性更好&#xff0c;分压电阻容易受到温度、老化的影响&#xff0c;长…

UDP协议(20250303)

1. UDP UDP:用户数据报协议&#xff08;User Datagram Protocol&#xff09;&#xff0c;传输层协议之一&#xff08;UDP&#xff0c;TCP&#xff09; 2. 特性 发送数据时不需要建立链接&#xff0c;节省资源开销不安全不可靠的协议 //一般用在实时性比较高…

基于https虚拟主机配置

一、https介绍 http 明文&#xff0c;80/tcp https 密文&#xff0c;443/tcp 二、安全性保障 1、数据安全性 数据加密 2、数据完整性 3、验证身份的真实性、有效性 三、数据安全性 手段&#xff1a;加密 发送方加密数据&#xff0c;接收方解密数据 对称加密算法 加密、解密数据…

机器学习(五)

一&#xff0c;多类&#xff08;Multiclass&#xff09; 多类是指输出不止有两个输出标签&#xff0c;想要对多个种类进行分类。 Softmax回归算法&#xff1a; Softmax回归算法是Logistic回归在多类问题上的推广&#xff0c;和线性回归一样&#xff0c;将输入的特征与权重进行…

概率论基础概念

前言 本文隶属于专栏《机器学习数学通关指南》&#xff0c;该专栏为笔者原创&#xff0c;引用请注明来源&#xff0c;不足和错误之处请在评论区帮忙指出&#xff0c;谢谢&#xff01; 本专栏目录结构和参考文献请见《机器学习数学通关指南》 正文 &#x1f3b2; 1. 随机事件 …

动漫短剧开发公司,短剧小程序搭建快速上线

在当今快节奏的生活里&#xff0c;人们的娱乐方式愈发多元&#xff0c;而动漫短剧作为新兴娱乐形式&#xff0c;正以独特魅力迅速崛起&#xff0c;成为娱乐市场的耀眼新星。近年来&#xff0c;动漫短剧市场呈爆发式增长&#xff0c;吸引众多创作者与观众目光。 从市场规模来看…

MySQL零基础教程15—简单的表连接(join)

在学习子查询的时候&#xff0c;我们已经感受到了&#xff0c;在一个语句中&#xff0c;通过访问不同表的数据最终获取我们想要的结果这种操作方式&#xff0c;实际上在mysql中&#xff0c;还有更加有趣的一个功能&#xff0c;就是表连接&#xff0c;同样是在查询数据的时候连接…

【AVRCP】深入剖析 AVRCP 命令体系:从单元到特定命令的全面解读

在蓝牙音频 / 视频远程控制规范&#xff08;AVRCP&#xff09;中&#xff0c;丰富的命令体系是实现设备间高效交互的关键。这些命令涵盖了单元命令、通用单元与子单元命令、特定命令等多个层面&#xff0c; 一、支持的单元命令 1.1 单元命令概述 AVRCP中支持的单元命令在设备…

物业管理系统源码 物业小程序源码

物业管理系统源码 物业小程序源码 一、基础信息管理 1. 房产信息管理 记录楼栋、单元、房间的详细信息&#xff08;面积、户型、产权等&#xff09;。 管理业主/租户的档案&#xff0c;包括联系方式、合同信息等。 2. 公共资源管理 管理停车场、电梯、绿化带、公…