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

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.kler.cn/a/227872.html

相关文章:

  • 利用飞书机器人进行 - ArXiv自动化检索推荐
  • 《大语言模型》综述学习笔记
  • 如何用 Groq API 免费使用 DeepSeek-R1 70B,并通过 Deno 实现国内访问
  • 计算机毕业设计Python+CNN卷积神经网络高考推荐系统 高考分数线预测 高考爬虫 协同过滤推荐算法 Vue.js Django Hadoop 大数据毕设
  • 【后端】Flask
  • C++,STL,【目录篇】
  • 【内置对象·js】
  • 从编程中理解:退一步海阔天空
  • Swift 入门之自定义类型的模式匹配(Pattern Matching)
  • 聊聊ClickHouse MergeTree引擎的固定/自适应索引粒度
  • Django模型(五)
  • Stable Diffusion 模型下载:EnvyHyperrealXL01
  • 《学成在线》微服务实战项目实操笔记系列(P1~P49)【上】
  • 《深入浅出Go语言》大纲
  • C++模板:函数模板
  • 幻兽帕鲁服务器自动重启备份-python
  • Python这些模块,你了解吗?
  • 2024年最新幻兽帕鲁服务器搭建教程
  • H5 加密(MD5 Base64 sha1)
  • vue3 之 组合式API—watch函数
  • 设计模式之工厂模式(工厂方法模式、抽象工厂模式)
  • 20240202金融读报1分钟小得
  • 基于Python查看TNT Go有线版串口数据以及屏幕电量
  • AI应用开发-git开源项目的一些问题及镜像解决办法
  • 开源协议介绍
  • Flink实战四_TableAPISQL