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

Vue2-动态组件案例

1.component介绍

说明:

  • Type: string | ComponentDefinition | ComponentConstructor

  • Explanation:

    • String: 如果你传递一个字符串给 is,它会被视为组件的名称,用于动态地渲染不同类型的组件。这是一个在运行时动态切换组件类型的常见用例。
    • ComponentDefinition: 你也可以传递组件定义对象,这是一个包含组件选项的对象。
    • ComponentConstructor: 可以传递组件的构造函数。

2.is写法

2.1 is

说明:加载单个组件直接可以不加:

<component is="DynamicMorning"></component>

组件 

<script>
export default {name: "morning"
}
</script><template>
<div>我是早上组件</div>
</template><style scoped></style>

2.2 :is 

说明:动态加载组件写法

    <template v-for="item in DynamicComponent"><component :is="item.type"></component></template>

2.3源码

<script>
import Dynamic from "@/components/Dynamic/index.vue";
import DynamicMorning  from "@/views/Dynamic/morning.vue";
import DynamicAfternoon from "@/views/Dynamic/afternoon.vue";
import DynamicEvening from "@/views/Dynamic/evening.vue";
export default {name: "index",components: {Dynamic,DynamicMorning,DynamicAfternoon,DynamicEvening},data() {return {// 组件数组componentArr: [{id: 1,type: 'Morning'}, {id: 2,type: 'Afternoon'}, {id: 3,type: 'Evening'}]}},computed:{// map遍历同时生成组件名字(类型是个数组)DynamicComponent(){return  this.componentArr.map(item=>{return {...item,type:'Dynamic'+item.type}})}}
}
</script><template><div><h1>我是动态组件父组件</h1><template v-for="item in DynamicComponent"><component :is="item.type"></component></template></div>
</template><style scoped></style>

 

3.源码

<script>
import Dynamic from "@/components/Dynamic/index.vue";
import DynamicMorning from "@/views/Dynamic/morning.vue";
import DynamicAfternoon from "@/views/Dynamic/afternoon.vue";
import DynamicEvening from "@/views/Dynamic/evening.vue";export default {name: "index",components: {Dynamic,DynamicMorning,DynamicAfternoon,DynamicEvening},data() {return {// 组件数组componentArr: [{id: 1,type: 'Morning'}, {id: 2,type: 'Afternoon'}, {id: 3,type: 'Evening'}]}},computed: {// map遍历同时生成组件名字(类型是个数组)DynamicComponent() {return this.componentArr.map(item => {return {...item,type: 'Dynamic' + item.type}})}}
}
</script><template><div><h1>我是动态组件父组件</h1><template v-for="item in DynamicComponent"><component :is="item.type"></component></template><!--    <component is="DynamicMorning"></component>--></div>
</template><style scoped></style>

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

相关文章:

  • 【源码】车牌检测+QT界面+附带数据库
  • 实战1-python爬取安全客新闻
  • 光栅化渲染:可见性问题和深度缓冲区算法
  • docker入门小结
  • LLM Agent发展演进历史(观看metagpt视频笔记)
  • Linux(操作系统)面经——part2
  • Flink系列之:WITH clause
  • JMeter直连数据库
  • Linux部署MySQL5.7和8.0版本 | CentOS和Ubuntu系统详细步骤安装
  • STL中set和multiset容器的用法(轻松易懂~)
  • Codeforces Round 915 (Div. 2)
  • C语言经典错误总结(三)
  • Ubuntu系统入门指南:基础操作和使用
  • MyBatis原理解读
  • Linux---文本搜索命令
  • Unity中Shader语义的理解
  • Flink系列之:Top-N
  • CSS的三大特性(层叠性、继承性、优先级---------很重要)
  • 飞天使-docker知识点10-docker总结
  • 旅游管理虚拟情景实训教学系统演示
  • Linux Shell——输入输出命令详解
  • MFC 第一个窗口程序
  • SQL语句的执行顺序怎么理解?
  • js解析.shp文件
  • 关于“Python”的核心知识点整理大全25
  • 代码随想录刷题题Day15
  • 软件设计师——信息安全(一)
  • git必须掌握:git远程变动怎么解决
  • Python里的时间模块
  • SCI一区级 | Matlab实现GWO-CNN-GRU-selfAttention多变量多步时间序列预测