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

买花网站代码企业网站制作

买花网站代码,企业网站制作,会计事务所,建设学校网站方案参考 vue动态组件<Component>与<KeepAlive> KeepAlive官网介绍 缓存之keep-alive的理解和应用 Vue3Vite KeepAlive页面缓存问题 vue多级菜单(路由)导致缓存(keep-alive)失效 vue3 router-view keeperalive对于同一路径但路径…

参考

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.ds6.com.cn/news/65186.html

相关文章:

  • 网站建设找什么工作seo工资水平
  • 网站后台登陆密码破解seo自学网视频教程
  • 泰州网站制作案例关键词在线查询
  • 网站是由哪些组成百度云搜索引擎入口
  • 易语言做检测网站更新搜索引擎优化是指
  • 什么网站需要备案免费网站的软件
  • 福州最好的网站建设网络公司谷歌搜索引擎网址
  • 广州 网站建设公司网络优化方案
  • 长春网络网站制作开发seo刷排名软件
  • 国外房屋设计网站资源企业网站排名优化价格
  • 上海市工程建设协会网站win10优化大师免费版
  • 服务器做网站好引流推广平台有哪些
  • 网站管理员的联系方式网站关键词优化推广哪家快
  • 触摸屏互动网站建设案例写文案接单平台
  • 网站轮播图居中代码怎么写网络销售怎么做才能做好
  • 建怎么网站比较赚钱网络营销的主要内容有哪些
  • 网站建设毕业论文参考文献推广页面制作
  • 网站 建设设计爱站网站seo查询工具
  • 网站的功能和作用是什么百度投诉热线中心客服
  • dns网站卫士 收录成都网络营销搜索推广
  • angularjs 做团购网站seo 首页
  • 网站做海康直播宁波seo网站服务
  • 安徽省工程建设网站网络企业推广
  • 安徽科技网站建设网站推广平台搭建
  • 国外做贸易网站百度人工服务热线
  • 怎么做类似淘宝一样的网站百度文库官网
  • 多少钱的英文翻译南宁seo优化公司
  • 福田做网站价格公司企业网站制作需要多少钱
  • 微网站开发服务网站制作模板
  • 届毕业设计代做网站网站优化主要优化哪些地方