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>

就能解决上面的问题。

相关推荐
記億揺晃着的那天5 小时前
Vue + Element UI 表格自适应高度如何做?
javascript·vue.js·ui
真的想不出名儿6 小时前
Vue 中 props 传递数据的坑
前端·javascript·vue.js
Queen_sy7 小时前
vue3 el-date-picker 日期选择器校验规则-选择日期范围不能超过七天
javascript·vue.js·elementui
技术钱8 小时前
vue3 两份json数据对比不同的页面给于颜色标识
前端·vue.js·json
Flash Dog9 小时前
【Vue】——路由
vue.js
羊羊小栈10 小时前
基于「多模态大模型 + BGE向量检索增强RAG」的航空维修智能问答系统(vue+flask+AI算法)
vue.js·人工智能·python·语言模型·flask·毕业设计
喝拿铁写前端10 小时前
Vue 组件通信的两种世界观:`.sync` 与普通 `props` 到底有什么不同?
前端·vue.js·前端框架
用户221520442780010 小时前
vue3组件间的通讯方式
前端·vue.js
tyro曹仓舒11 小时前
Vue单文件组件到底需不需要写name
前端·vue.js
閞杺哋笨小孩12 小时前
Vue3 点击指令(防抖 / 节流)
前端·vue.js