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

净水设备 技术支持 东莞网站建设要服务网站建设

净水设备 技术支持 东莞网站建设,要服务网站建设,黄骅市网站建设价格,app优化排名动态组件的基本使用 动态组件(Dynamic Components)是一种在 Vue 中根据条件或用户输入来动态渲染不同组件的技术。 在 Vue 中使用动态组件,可以使用 元素,并通过 is 特性绑定一个组件的名称或组件对象。通过在父组件中改变 is 特…

动态组件的基本使用

动态组件(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.yayakq.cn/news/357695/

相关文章:

  • 网站运行维护方案网页设计与制作课程的建设历程
  • 做网站 360梅县区住房和城乡规划建设局官方网站
  • 株洲网站建设技术托管wordpress主题 ipcme
  • 汕头公司建站模板建网站解决方案
  • 企业网站建设流程与方法 论文常州房地产网站建设
  • 搭建网站内链系统上海工程信息网
  • 部门网站建设目的wordpress外观自定义
  • 获取网站访问量优质公司网站
  • 做网站定金是多少东莞地图
  • 山东商祺网站建设优化个人简介代码网页制作
  • 嘉兴 企业网站 哪家比较好的设计网站推荐
  • 做交友信息网站可行么软件公司如何找客户
  • 豪华网站建设小学手工制作100种
  • 我的世界做图片网站网站开发实践感想
  • 做视频免费模板下载网站做网站流量怎么解决
  • 彩票网站怎么做的手机网站与普通网站的区别
  • 南宁网站建设智能优化wordpress管理员帐号
  • 驾校做网站visual studio 网站开发
  • 网站建设 长沙绿色网站模板
  • aspcms手机网站源码网站编程培训机构排名前十
  • 什么叫网站建设怎么更改自动目录的格式
  • 电子商务网站建设与设计杭州工程建设网
  • 泵网站建设网页设计的模板
  • 正规的建网站公司网站备案找谁
  • 广东网站seo网站被挂黑链对网站有啥影响
  • 保定网站seo服务织梦做的网站很老
  • 在淘宝上做网站如何付费0元代理在家就可以做
  • 网站数据库分离怎么做帮客户做ppt什么的在哪个网站
  • 有哪些做网站的网站做网站背景图片浪漫爱情
  • 网站打开很慢wordpress怎么做站群