vue3中使用keepalive和component

vue3和vue2中有些差异,直接上代码:

javascript 复制代码
  <KeepAlive include="aComponent">
          <component 
            :is='compList[active]'
            @goDetail="goDetail"
            @back="back" 
          />
      </KeepAlive>  
<script setup lang="ts">
/**
 * 将事件从报告图表中抛上来,传递具体数据
 */
import { ref ,markRaw} from 'vue'
import aComponent from './Acomponent.vue'
import bcomponent from './Bcomponent.vue'

const dataDownConfig = ref(null);
const active = ref('aComponent');
const compList = ref({
  aComponent: markRaw(aComponent ),
  bComponent: markRaw(bComponent )
})
// 通过切换active的值来切换组件
const goDetail = (data: any) => {
  active.value = 'aComponent '
}
const back = () => {
  active.value = 'bcomponent '
}
</script>      

// Acomponent.vue (给组件加名字)

javascript 复制代码
<script lang="ts" setup>
defineOptions({
  name: 'aComponent '
})
</script>
相关推荐
2601_9516152024 分钟前
网站模板 网站网页模板下载
前端·网络·html
酷炫码神1 小时前
Day 1|HTML5:一个商城首页的骨架,从 DOCTYPE 到语义化
前端·html·html5
AI源码1 小时前
鲜花商城(HTML 版)
前端·html
hiahiahia1232 小时前
Message 和对话
服务器·前端·数据库
雪芽蓝域zzs2 小时前
第 7 节:多边形点击弹窗 + 清空绘制按钮
vue.js
溪语流沙2 小时前
【Web全栈进阶】Composition API与组件通信:props深入、emit、插槽
前端·css·vue.js
ZoeLandia2 小时前
TypeScript 03:泛型与工具类型
前端·typescript
code_Bo2 小时前
从策划到上线:我和 AI 一起做了个微信记账小程序
前端·ai编程·cursor
Shirley~~2 小时前
Three.js加载Gltf模型并播放动画
前端·3d
Data analyse4562 小时前
Session超时与浏览器休眠的计时校验
大数据·前端·数据库·数据分析