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 分钟前
如何用Vite实现Vue组件的按需打包和远程加载
前端·vue.js·前端框架
用户9004633704019 分钟前
用Gemini搞定Vue报错和语法异常的问题
vue.js
小兔崽子去哪了3 小时前
Vue3 + Pinia 集成 IGV.js 实现 BAM 文件在线浏览
javascript·vue.js·后端
OpenTiny社区21 小时前
🎨 看完 GenUI SDK 源码我悟了!
前端·vue.js·github
mqcode1 天前
你项目里的 axios,封对了吗?从裸用到生产级的四步进化
vue.js·axios
Linsk1 天前
组件 = 模板 + 业务逻辑
java·前端·vue.js
前端啊1 天前
告别 el-table 打印难题,vue3-print-el-table 来了!
前端·vue.js
AprChell1 天前
低代码设计器和低代码设计引擎架构综述
前端·vue.js·低代码
Ruihong1 天前
🎉 VuReact 1.9.0 发布,支持 Vue 3.4 defineModel 编译到 React
vue.js·react.js·面试
英勇无比的消炎药1 天前
TinyRobot 源码深度分析:OpenTiny 的 AI 对话组件库
前端·vue.js·github