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

vue3使用is动态切换组件报错Vue received a Component which was made a reactive object.

vue3使用is动态切换组件,activeComponent用ref定义报错

Vue received a Component which was made a reactive object. This can lead to unnecessary performance overhead, and should be avoided by marking the component with `markRaw` or using `shallowRef` instead of `ref`

 我们需要使用 shallowRef 替代 ref 来避免报错。shallowRef 创建的引用不会将组件标记为响应式对象,从而避免了潜在的性能开销。 

<button @click="switchComponent('componentA')">Component A</button>
<button @click="switchComponent('componentB')">Component B</button>
<component :is="currentComponent"></component><script setup name="swtichComponent">
import { computed, ref, markRaw } from 'vue'
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';const currentComponent = ref(markRaw(ComponentA));function switchComponent(component) {if (component === 'componentA') {currentComponent.value = markRaw(ComponentA);} else if (component === 'componentB') {currentComponent.value = markRaw(ComponentB);}
}</script>

切换组件不能做全局相关操作,例如关闭当前页面,需要子传父调用

父:
<component :is="activeComponent" @close="handleClose" />function handleClose() {window.close()
}子:
const emits = defineEmits(['close'])
const closeHandle = () => {emits('close')
}

http://www.lryc.cn/news/291903.html

相关文章:

  • React16源码: React中LegacyContext的源码实现
  • Gin 框架之jwt 介绍与基本使用
  • 从[redis:LinkedList]中学习链表
  • Prometheus+grafana配置监控系统
  • Linux之安装配置CentOS 7
  • 神经网络与深度学习Pytorch版 Softmax回归 笔记
  • git学习及简单maven打包
  • 如何用MapTalks IDE来发布网站?
  • 我用selenium开发了一个自动创建任务,解放重复性工作
  • 安卓11修改HDMI自适应分辨率
  • Linux实验记录:使用Apache的虚拟主机功能
  • 分布式空间索引了解与扩展
  • Set和Map的应用场景
  • 小白级教程,10秒开服《幻兽帕鲁》
  • IDEA 构建开发环境
  • 归并排序----C语言数据结构
  • 【网站项目】065健康综合咨询问诊平台
  • Adobe Camera Raw forMac/win:掌控原始之美的秘密武器
  • OpenHarmony—开发及引用静态共享包(API 9)
  • 测试面试题常见题
  • 代码随想录算法训练营第六天 - 哈希表part02
  • 【Javaweb程序设计】【C00165】基于SSM的高考志愿辅助填报系统(论文+PPT)
  • 海外云手机为什么吸引用户?
  • 将`List<String>`转换为`List<Long>`
  • 【Unity3D小功能】Unity3D中Text使用超链接并绑定点击事件
  • MyBatis-Plus CRUD 接口
  • 在JVM中,Java对象是如何创建、存储和访问的?
  • C++类和对象之进击篇
  • ElementUI 组件:Container 布局容器
  • 小米商城服务治理之客户端熔断器(Google SRE客户端熔断器)