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 小时前
vue复杂数据类型多层嵌套的监听
前端·javascript·vue.js
GoodStudyAndDayDayUp1 小时前
gitlab+portainer 实现Ruoyi Vue后端CI/CD
vue.js·ci/cd·gitlab
.生产的驴2 小时前
Vue3 加快页面加载速度 使用CDN外部库的加载 提升页面打开速度 服务器分发
运维·服务器·前端·vue.js·分布式·前端框架·vue
我是Superman丶5 小时前
【技巧】前端VUE用中文方法名调用没效果的问题
前端·javascript·vue.js
斯~内克5 小时前
Vue 3 中 watch 的使用与深入理解
前端·javascript·vue.js
松树戈9 小时前
plus-ui&RuoYi-Vue-Plus 基于pgSql本地运行实践
前端·vue.js·spring boot·ui
Stella252111 小时前
【Vue】CSS3实现关键帧动画
前端·vue.js·css3
素雪风华12 小时前
构建RAG混合开发---PythonAI+JavaEE+Vue.js前端的实践
java·vue.js·python·ai·语言模型·llms·qwen千问大模型
这个一个非常哈14 小时前
VUE篇之自定义组件使用v-model
前端·javascript·vue.js
热爱前端的小君同学15 小时前
长按拖拽移动的vue3组件
前端·javascript·vue.js