vue3动态组件组件shallowRef包裹问题

在vue3做tabs切换功能的时候,如果导入的组件不适用shallowRef包裹会显示下面的警告:

javascript 复制代码
Home.vue?t=1708401434509:43 [Vue warn]: Vue received a Component that 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`. 
Component that was made reactive:  {__hmrId: '2afd162d', __file: 'D:/v3code/my-big-charts/src/components/Comp7.vue', render: ƒ} 
  at <Home onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< Proxy(Object) {__v_skip: true} > > 
  at <RouterView> 
  at <App>

根据提示代码修改如下:

javascript 复制代码
<script setup>
import {ref,reactive,readonly,computed,watch,watchEffect,markRaw,shallowRef} from 'vue'
import Comp7 from '../components/Comp7.vue'
import Comp6 from '../components/Comp6.vue'
const activeComp = shallowRef(Comp6)
const tab = reactive([{
    name: "A组件",
    comName: shallowRef(Comp6)
}, {
    name: "B组件",
    comName: shallowRef(Comp7
}])
const change = index=>{
    activeComp.value = tab[index].comName
}
</script>
<template>
    <ul>
        <li v-for="(item,index) in tab" :key="item.name" @click="change(index)">{{ item.name }}</li>
    </ul>
    <div>
        <component :is="activeComp"></component>
    </div>
</template>

就能解决上面的问题。

相关推荐
前端那点事1 小时前
彻底弄懂async/await!解决回调地狱,Vue异步开发必备(超全实战)
前端·vue.js
A_nanda1 小时前
VS2022安装QT6.5.3后,如何更新项目配置
前端·javascript·vue.js
Lkstar2 小时前
读懂 Vue 双端 Diff 算法:从源码到原理,一篇彻底搞懂
vue.js
早起傻一天~G3 小时前
vue2+element-UI表单封装
前端·vue.js·ui
Live&&learn3 小时前
Vue项目打包后内联字符串不显示的原因
前端·javascript·vue.js
宁雨桥3 小时前
深入剖析Vue2与Vue3响应式原理:从Object.defineProperty到Proxy的演进
前端·vue.js
ejinxian4 小时前
Rust的GUI方案中,Slint、Azul、egui、iced、Druid、Tauri
前端·javascript·vue.js
Python私教4 小时前
ShadcnVueAdmin 的国际化是怎么实现的
前端·javascript·vue.js
MXN_小南学前端17 小时前
watch详解:与computed 对比以及 Vue2 / Vue3 区别
前端·javascript·vue.js
2601_9498146918 小时前
Docker部署Spring Boot + Vue项目
vue.js·spring boot·docker