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

【Vue3】动态组件

动态组件的基本使用

动态组件(Dynamic Components)是一种在 Vue 中根据条件或用户输入来动态渲染不同组件的技术。

在 Vue 中使用动态组件,可以使用 元素,并通过 is 特性绑定一个组件的名称或组件对象。通过在父组件中改变 is 特性的值,可以动态切换渲染的组件。

第一种写法

A.vue

<template><div>A component</div>
</template><script setup lang="ts"></script><style scoped></style>

B.vueC.vue 同理

APP.vue

<template><div style="display: flex;"><!-- class可以写两个,一个静态,一个动态 --><div @click="switchCom(item, index)" :class="[active == index ? 'active' : '']" class="tabs"v-for="(item, index) in data"><div>{{ item.name }}</div></div></div><component :is="comId"></component>
</template><script setup lang="ts">
import { ref, reactive } from 'vue';
import AVue from './components/A.vue'
import BVue from './components/B.vue'
import CVue from './components/C.vue'
// 这里不需要将对象中所有数据变为响应式,可以使用ref
const comId = ref(AVue)
const active = ref(0)const switchCom = (item, index) => {comId.value = item.comactive.value = index
}const data = reactive([{name: 'A',com: AVue},{name: 'B',com: BVue},{name: 'C',com: CVue}
])
</script><style lang="scss" scoped>
.active {background: blueviolet;
}.tabs {border: 1px solid #ccc;padding: 5px 10px;margin: 5px;cursor: pointer;}
</style>

在这里插入图片描述

第二种写法

APP.vue

<template><div style="display: flex;"><!-- class可以写两个,一个静态,一个动态 --><div @click="switchCom(item, index)" :class="[active == index ? 'active' : '']" class="tabs"v-for="(item, index) in data"><div>{{ item.name }}</div></div></div><component :is="comId"></component>
</template><script setup lang="ts">
// markRaw:作用:标记一个对象,使其永远不会再成为响应式对象。
import { ref, reactive, markRaw, shallowRef } from 'vue';// 这里不需要将对象中所有数据变为响应式,可以使用ref
const comId = shallowRef('AVue')
const active = ref(0)const switchCom = (item, index) => {comId.value = item.comconsole.log(comId.value);active.value = index
}const data = reactive([{name: 'A',com:'AVue'},{name: 'B',com:'BVue'},{name: 'C',com:'CVue'}
])
</script><script lang="ts">
import AVue from './components/A.vue'
import BVue from './components/B.vue'
import CVue from './components/C.vue'export default {components: {AVue,BVue,CVue}
}
</script><style lang="scss" scoped>
.active {background: blueviolet;
}.tabs {border: 1px solid #ccc;padding: 5px 10px;margin: 5px;cursor: pointer;}
</style>

性能优化

上述第一种写法代码会出现警告
在这里插入图片描述
输出 comId 的值,出现 comId 的属性被劫持,出现性能浪费
在这里插入图片描述

解决方法

使用markRawshallowRef这两个API

App.vue

<template><div style="display: flex;"><!-- class可以写两个,一个静态,一个动态 --><div @click="switchCom(item, index)" :class="[active == index ? 'active' : '']" class="tabs"v-for="(item, index) in data"><div>{{ item.name }}</div></div></div><component :is="comId"></component>
</template><script setup lang="ts">
// markRaw:作用:标记一个对象,使其永远不会再成为响应式对象。
import { ref, reactive, markRaw, shallowRef } from 'vue';
import AVue from './components/A.vue'
import BVue from './components/B.vue'
import CVue from './components/C.vue'
// 这里不需要将对象中所有数据变为响应式,可以使用ref
const comId = shallowRef(AVue)
const active = ref(0)const switchCom = (item, index) => {comId.value = item.comconsole.log(comId.value);active.value = index
}const data = reactive([{name: 'A',com: markRaw(AVue)},{name: 'B',com: markRaw(BVue)},{name: 'C',com: markRaw(CVue)}
])
</script><style lang="scss" scoped>
.active {background: blueviolet;
}.tabs {border: 1px solid #ccc;padding: 5px 10px;margin: 5px;cursor: pointer;}
</style>

再次输出 comId 的值,解决性能浪费的问题
在这里插入图片描述

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

相关文章:

  • Java超级玛丽小游戏制作过程讲解 第五天 创建并完成常量类04
  • 设置浏览器兼容
  • Java # List
  • git原理与使用
  • 【C语言题解】将一句话的单词进行倒置,标点不倒置。
  • Postman 的简单使用
  • 在CentOS7安装部署GitLab服务
  • 订单系统就该这么设计,稳的一批~
  • Agents改变游戏规则,亚马逊云科技生成式AI让基础模型加速工作流
  • 详细教程:如何搭建废品回收小程序
  • 什么是双亲委派机制?
  • Mageia 9 RC1 正式发布,Mandriva Linux 发行版的社区分支
  • ChatGPT: 人机交互的未来
  • Linux 常用操作命令
  • 24届近5年重庆邮电大学自动化考研院校分析
  • 如何对oracle和mysql进行 分区分表
  • Windows下安装Sqoop
  • Chrome 谷歌浏览器,自动填充密码,提示需要输入电脑开机密码问题
  • Java技术整理(3)—— 多线程并发篇
  • 2023热门跨境电商平台哪个入驻条件适合新手?
  • 【MFC】05.MFC第一大机制:程序启动机制-笔记
  • 小程序动画 animation 的常规使用
  • Swift 周报 第三十四期
  • [虚幻引擎] UE DTBase64 插件说明 使用蓝图对字符串或文件进行Base64加密解密
  • Jmeter组件作用域及执行顺序
  • 题目:2309.兼具大小写的最好英文字母
  • RISC-V公测平台发布:如何在SG2042上玩转OpenMPI
  • Jenkins 使用
  • 使用go-zero快速构建微服务
  • Java开发 - Redis事务怎么用?