KeepAlive与RouterView缓存

ops/2024/12/19 14:07:23/

参考

vue动态组件<Component>与<KeepAlive>

KeepAlive官网介绍

缓存之keep-alive的理解和应用

Vue3+Vite KeepAlive页面缓存问题

vue多级菜单(路由)导致缓存(keep-alive)失效

vue3 router-view keeperalive对于同一路径但路径参数不同

  • Vue keep-alive,同一个路由组件参数不同,如何分别缓存状态

  • Vue路由 – 相同路由路径参数不同,复用组件问题

文章目录

  • 参考
  • 效果
    • main.js
      • router.js
    • App.vue
      • Home.vue
      • Chat.vue
        • ChatDetail.vue

效果

在这里插入图片描述

main.js

import { createApp } from 'vue'import './style.css'import App from './App.vue'
import router from './router'import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'const app = createApp(App)
app.use(router)
app.use(ElementPlus)
app.mount('#app')

router.js

import { createWebHistory, createRouter } from "vue-router"import Home from '@/views/Home.vue'
import Chat from '@/views/Chat.vue'
import ChatDetail from '@/views/ChatDetail.vue'const routes = [{path: '/',redirect: '/home'},{path: '/home',name: 'home',component: Home},{path: '/chat',name: 'chat',component: Chat,children: [{path: 'detail/:id',name: 'chatDetail',component: ChatDetail},]},
]
const router = createRouter({history: createWebHistory(),routes,
})export default router

App.vue

<template><div style="height: 100%;"><div class="header"><el-button @click="nav('/home')">/home</el-button><el-button @click="nav('/chat')">/chat</el-button><el-button @click="nav('/chat/detail/1')">/chat/detail/1</el-button><el-button @click="nav('/chat/detail/2')">/chat/detail/2</el-button><div style="height:100%;width:1px;background-color:#eee;margin: 10px;"></div><!-- 这里的缓存的意思是: 当从ChatDetail组件切到Home组件时, Chat组件实例里的数据还是否缓存 --><el-button @click="cachedComponents = ['Chat']">缓存chat</el-button><el-button @click="cachedComponents = []">取消缓存chat</el-button>{{cachedComponents}}</div><!-- 当在home组件与chat组件切换时, 被切走的组件会被销毁, 切过去的组件会被创建 --><!-- <router-view class="container-wrapper"/> --><!-- home组件和chat组件都仅仅被创建了1次, 当在home组件与chat组件切换时, home组件与chat组件并未被销毁或创建 --><!-- <router-view v-slot="{ Component }"><keep-alive><component :is="Component" class="container-wrapper"/></keep-alive></router-view> --><!-- home组件仅被创建了1次并且切走时会被缓存下来不会被销毁, 切过来时不会重新创建; 而chat组件被切走会被销毁, 切到chat组件时, chat组件会被创建;这里的include指的是 组件名称, 而不是路由名称 --><!-- <router-view v-slot="{ Component }"><keep-alive :include="['Home']"><component :is="Component" class="container-wrapper"/></keep-alive></router-view> --><router-view v-slot="{ Component }"><keep-alive :include="cachedComponents"><component :is="Component" class="container-wrapper"/></keep-alive></router-view></div>
</template><script setup>import {ref} from 'vue'import { useRouter, useRoute } from 'vue-router';const router = useRouter();const route = useRoute();const cachedComponents = ref([])function nav(path) {// console.log(path);router.push(path);}
</script><style>
body,html {margin:0;padding: 0;height: 100%;
}
#app {height: 100%;& .header {height: 51px;line-height: 51px;padding: 0 20px;border-bottom: 1px solid #eee;display: flex;align-items: center;justify-content: flex-start;}& .container-wrapper {height: calc(100% - 52px);}
}
</style>

Home.vue

<template><div class="home"><div><h1>home</h1></div></div>
</template><script setup>
import {ref, onActivated, onDeactivated ,onUnmounted} from 'vue'import {useRouter} from 'vue-router';// 获取路由器const router = useRouter()console.log('【Home组件】创建');onUnmounted(()=>{console.log('【Home组件】销毁');})</script><style lang="scss">
.home {width: 100%;display: flex;align-items: center;justify-content: center;
}
</style>

Chat.vue

<template><div class="container"><div class="left"><el-button @click="nav('/home')">/home</el-button><el-button @click="nav('/chat/detail/1')">/chat/1</el-button><el-button @click="nav('/chat/detail/2')">/chat/2</el-button></div><div class="right"><!-- <router-view/> --><!-- <router-view v-slot="{ Component }"><keep-alive><component :is="Component"/></keep-alive></router-view> --><!-- 这里给component添加1个key之后, 就可以根据路由路径来缓存组件实例了: 1个路由路径对应1个组件实例 --><router-view v-slot="{ Component }"><keep-alive><component :is="Component" :key="route.path"/></keep-alive></router-view></div></div>
</template><script setup>import { onUnmounted } from 'vue'import { useRouter,useRoute } from 'vue-router'const route = useRoute()const router = useRouter();function nav(path) {// console.log(path);router.push(path);}console.log('【Chat组件】创建');onUnmounted(()=>{console.log('【Chat组件】销毁');})
</script><style lang="scss" scoped>
.container {display: flex;.left {width: 220px;border-right: 1px solid #eee;display: flex;flex-direction: column;align-items: center;padding-top: 10px;background-color: #f2f2f2;.el-button {margin-bottom: 10px;width: 80%;}}.right {flex: 1;padding: 20px;background-color: #e1e1e1;}   
}
.el-button+.el-button {margin-left: 0;
}
</style>
ChatDetail.vue
<template><div class="chat-box"><div class="header"><h1>会话{{route.params.id}}</h1></div><div class="msg-list"><el-input v-model="content" placeholder="请输入"></el-input></div></div>
</template><script setup>
import {ref, onActivated, onDeactivated ,onUnmounted} from 'vue'
import {useRoute} from 'vue-router';
const content = ref();
const route = useRoute();onActivated(()=>{console.log('---【ChatDetail组件】激活---');
});
onDeactivated(()=>{console.log('---【ChatDetail组件】取消激活---');
});console.log('---【ChatDetail组件】创建---');onUnmounted(()=>{console.log('---【ChatDetail组件】销毁---');
})</script><style lang="scss" scoped>.chat-box {display: flex;flex-direction: column;height: 100%;.msg-list {flex: 1;}}.header {border: 2px solid #eee;line-height: 68px;height: 68px;h1 {margin: 0;}}
</style>

http://www.ppmy.cn/ops/143197.html

相关文章

修改uniapp下拉刷新圆圈颜色

直接看图 修改前就是常规的绿色 自定义更符合我们的软件 直接说方法 修改 在App.vue的style样式里添加一行 .uni-page-refresh--refreshing .uni-page-refresh__path{stroke:#FF2442; }我是通过 不执行 uni.stopPullDownRefresh(); 下拉刷新 之后通过F12看出来的 希望可以帮…

一键学懂BurpSuite(8)

声明&#xff01; 学习视频来自B站up主 泷羽sec 有兴趣的师傅可以关注一下&#xff0c;如涉及侵权马上删除文章&#xff0c;笔记只是方便各位师傅的学习和探讨&#xff0c;文章所提到的网站以及内容&#xff0c;只做学习交流&#xff0c;其他均与本人以及泷羽sec团队无关&#…

cmake中 macro和函数的区别

语法形式上的区别 macro&#xff08;宏&#xff09;&#xff1a; 定义宏使用macro关键字&#xff0c;语法格式为macro(<macro_name> [arg1 [arg2 [...]]])。例如&#xff1a;macro(my_macro arg1 arg2)message("The first argument is ${arg1}")message("…

娱乐五子棋(附加源码)

一写在开头 上期代码主要实现瀑布流功能&#xff0c;本期就来实现五子棋小游戏&#xff0c;开发久了很多功能都是通过框架组件库来完成&#xff0c;但是如果组件满足不了开发需求&#xff0c;还需要开发人员手动封装组件&#xff0c;专门出这样一期文章&#xff0c;通过原生js实…

rpc设计的再次思考20251215(以xdb为核心构建游戏框架)

1.服务提供者注册的方式 // 表明这是一个服务提供者&#xff0c;ServerType 和 ServerId从application.properties中读取 // 而且只有当当前服务是Game时&#xff0c;才生效。 或者 条件注解??? RpcProvider(typeServerType.Game) public class GameProvider{MsgReceiver…

04_ok_java_websocket_端口转发_将服务器的流端口转到本地

客户端socket package com.example.filedemo.controller; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; import java.io.File; import java.io.IOException; import java.net.URI; import java.util.Base64; imp…

【论文笔记】CLIP-guided Prototype Modulating for Few-shot Action Recognition

&#x1f34e;个人主页&#xff1a;小嗷犬的个人主页 &#x1f34a;个人网站&#xff1a;小嗷犬的技术小站 &#x1f96d;个人信条&#xff1a;为天地立心&#xff0c;为生民立命&#xff0c;为往圣继绝学&#xff0c;为万世开太平。 基本信息 标题: CLIP-guided Prototype Mo…

Docker Desktop 4.37.0版本支持 命令行启动了

更新日志 Docker Desktop 4.37.0版本 支持命令行了&#xff0c;参考&#xff1a;https://docs.docker.com/desktop/release-notes/ 如下图所示 命令 点上图中的command line 可以看到相关的命令&#xff0c;如下图&#xff1a; 示例 以start为例&#xff0c;如下图所示&…