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

荔湾区做网站公司保定seo推广公司

荔湾区做网站公司,保定seo推广公司,如何建设管理好法院网站,长沙网站建设大概多少钱动态组件的基本使用 动态组件(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.zhongyajixie.com/news/45604.html

相关文章:

  • 罗岗网站建设公司今日nba数据帝
  • 网站视差怎么做病毒式营销
  • 网页设计茶叶网站建设外贸网络推广公司
  • 扬州做网站的科技公司网址大全名称
  • 品牌型网站制作价格快速排名新
  • 网站js聊天代码网络营销策划方案的目的
  • 做电子元器件销售什么网站好seo代码优化包括哪些
  • 政府网站建设的概念免费广告制作软件
  • 做八闽最好的中学网站百度手机助手
  • 免费注册域名网站推荐网络营销策划方案框架
  • 网上学习做网站客服网站搭建
  • 网站子目录怎么做反向代理设置北京正规seo搜索引擎优化价格
  • 法院网站建设调研网页设计一般用什么软件
  • 网站建站行业舆情网站直接打开的软件
  • 青岛城阳软件网站开发关键词seo资源
  • 安妮导刊 wordpress郑州网络优化实力乐云seo
  • 西城网站建设公司武汉百捷集团百度推广服务有限公司
  • 自己接单赚钱app郑州网络seo公司
  • 做建材的哪些网站线上广告推广
  • 线上推广的意义沈阳百度seo关键词优化排名
  • 网站的点击率怎么查爱站网seo工具
  • rar在线解压缩网站sem优化推广
  • 咸阳鑫承网站建设宁波seo关键词排名优化
  • 网站空间 按流量计费平台优化是指什么
  • 做网站用什么软件语言宁德市教育局官网
  • 做外贸的网站b2c云搜索系统
  • 创建网站的四个步骤是怎样在百度上推广
  • wordpress 跳转首页app排名优化
  • 临沂企业建站程序b2b
  • 深圳品牌设计公司介绍如何利用seo赚钱