当前位置: 首页 > news >正文

vue3使用动态component

使用场景:

多个组件通过component标签挂载在同一个组件中,通过触发时间进行动态切换。vue3与vue2用法不一样,这里有坑!

使用方法:

1.通过vue的defineAsyncComponent实现挂载组件

2.component中的is属性

父组件:

<template><div><div v-for="item in person.data" :key="item" @click="btn(item)">{{ item.name }}</div><h1>下面为动态组件</h1><component :is="person.componen"> </component></div>
</template><script setup>
import { reactive, onMounted, defineAsyncComponent } from "vue";
const One = defineAsyncComponent(() => import("./One.vue"));
const Two = defineAsyncComponent(() => import("./Two.vue"));const person = reactive({componen: "",data: [{ type: "one", name: "显示组件一" },{ type: "two", name: "显示组件二" },],
});
function btn(item) {if (item.type == "one") person.componen = One;if (item.type == "two") person.componen = Two;
}onMounted(() => {});
</script>

子组件:

<template><div>组件一</div><el-input v-model="person.input"></el-input>
</template><script setup>
import { ref, reactive, onMounted, computed, watch } from "vue";const person = reactive({ input: "" });
onMounted(() => {console.log("组件一");
});
</script>
<style scoped lang='less'>
</style>

效果:

这里会有警告: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`. (Vue收到一个组件,该组件被设置为反应对象。这可能会导致不必要的性能开销,应该通过用“markRaw”标记组件或使用“shallowRef”而不是“ref”来避免。)

        

解决方法:

1.使用shallowRef替换响应式

<template><div><div v-for="item in person.data" :key="item" @click="btn(item)">{{ item.name }}</div><h1>下面为动态组件</h1><keep-alive><component :is="componen"> </component></keep-alive></div>
</template><script setup>
import { reactive, onMounted, defineAsyncComponent, shallowRef } from "vue";
let componen = shallowRef(null);
const Two = defineAsyncComponent(() => import("./Two.vue"));
const One = defineAsyncComponent(() => import("./One.vue"));
let obj = shallowRef({Two,One,
});
const person = reactive({data: [{ type: "one", name: "显示组件一" },{ type: "two", name: "显示组件二" },],
});
function btn(item) {if (item.type == "one") componen.value = obj.value.One;if (item.type == "two") componen.value = obj.value.Two;
}onMounted(() => {});
</script>
<style scoped lang='less'>
</style>

http://www.lryc.cn/news/252640.html

相关文章:

  • 单机游戏推荐:巨击大乱斗 GIGABASH 中文安装版
  • 计算机系统启动过程
  • DedeCms后台文章列表文档id吗?或者快速定位id编辑文章
  • 【开发问题解决方法记录】03.dian
  • QT之QString
  • 常见的几种计算机编码格式
  • 3D旋转tab图
  • openGL 三:矩阵和向量
  • Socket和Http的通讯原理,遇到攻击会受到哪些影响以及如何解决攻击问题。
  • 【springboot】整合redis
  • 回溯和分支算法
  • 深入理解:指针变量的解引用 与 加法运算
  • Docker 镜像构建的最佳做法
  • 工作上Redis安装及配置
  • 电商项目之Web实时消息推送(附源码)
  • 上机实验四 哈希表设计 西安石油大学数据结构
  • Ubuntu22.04 交叉编译mp4V2 for Rv1106
  • 缓存穿透、击穿、雪崩
  • (1w字一篇理解透Unsafe类)Java魔法类:Unsafe详解
  • Python的正则表达式使用
  • Elasticsearch:评估 RAG - 指标之旅
  • 【2023.12.4练习】数据库知识点复习测试
  • 【wvp】测试记录
  • 【若依框架实现上传文件组件】
  • 玩转大数据5:构建可扩展的大数据架构
  • 【华为数据之道学习笔记】非数字原生企业的特点
  • Kubernetes学习笔记-Part.01 Kubernets与docker
  • k8s学习
  • 测试:JMeter和LoadRunner比较
  • (C语言)通过循环按行顺序为一个矩阵赋予1,3,5,7,9,等奇数,然后输出矩阵左下角的值。