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>

就能解决上面的问题。

相关推荐
前端炒粉2 小时前
长会话虚拟滚动与渲染优化
前端·javascript·vue.js
sibylyue3 小时前
基于 Vben Admin 框架的 Vue 3 前端项目配置文件及常用命令
前端·javascript·vue.js
小蒜学长6 小时前
vue旅游攻略网站(代码+数据库+LW)
java·数据库·vue.js·spring boot·后端·旅游
sir.山1 天前
在 Vue 3 项目中使用 Mock 数据
前端·vue.js·vue3·vite
你别说话了2 天前
Vue项目解决跨域
前端·javascript·vue.js
学习星球2 天前
Solid.js 实战:拆解官方 RealWorld 项目
开发语言·javascript·vue.js
尚可签2 天前
基于 Spring Boot + Vue 的 AI 智能在线订餐系统
vue.js·人工智能·spring boot
MacroZheng2 天前
腾讯又开源了一个新项目,用起来真优雅!
前端·vue.js·人工智能
张人玉2 天前
基于 Vue 3 + ECharts + Three.js + Express + SQLite——智慧城市数字孪生可视化平台
javascript·vue.js·信息可视化·sqlite·echarts
山川志~2 天前
Vue + Element UI 实战:如何实现表格时间、金额字段排序
vue.js·ui·elementui