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

vue、uniapp实现组件动态切换

在Vue中,通过使用动态组件,我们可以实现组件的动态切换,从而达到页面的动态展示效果。

vue 中 component组件 is属性

 功能描述 

例如:有多个tabs标签,如:推荐、热点、视频等。用户点击标签就会切换到对应组件

vue2版

<template><!-- 标签数据 --><!-- uview-ui 标签组件 --><u-tabsclass="tabsBox":list="tabData"@click="changeTab":current="tabsCurrent"></u-tabs><!-- 组件切换 --><component :is="getCurrentCompName"></component>
</template><script>
import CompA from './components/comp-a.vue'
import CompB from './components/comp-b.vue'
import CompC from './components/comp-c.vue'export default {data() {return {tabsCurrent: 0,tabsList: [],}},computed: {getCurrentCompName() {let currentCompName = ''switch (this.tabsCurrent) {case 1:currentCompName = 'CompB'breakcase 2:currentCompName = 'CompC'breakdefault:currentCompName = 'CompA'}return currentCompName},},methods: {toggle(index) {this.tabsCurrent = index},},
}
</script>

vue3版

<template><!-- 标签数据 --><!-- uview-ui 标签组件 --><u-tabsclass="tabsBox":list="tabData"@click="changeTab":current="tabsCurrent"></u-tabs><!-- 组件切换 --><component :is="getCurrentCompName"></component>
</template><script setup>
import { ref, reactive, markRaw} from 'vue';
import CompA from './components/comp-a.vue';
import CompB from './components/comp-b.vue';
import CompC from './components/comp-c.vue';const tabsCurrent = ref(0);
const tabsList = ref([]);const getCurrentCompName = () => {let currentCompName = '';switch (tabsCurrent.value) {case 1:currentCompName = markRaw(CompB);break;case 2:currentCompName = markRaw(CompC);break;default:currentCompName = markRaw(CompA);}return currentCompName;
};const toggle = (index) => {tabsCurrent.value = index;
};
</script>

或者

<template><!-- 标签数据 --><!-- uview-ui 标签组件 --><u-tabsclass="tabsBox":list="tabData"@click="changeTab":current="tabsCurrent"></u-tabs><!-- 组件切换 --><component :is="currentComp"></component>
</template><script setup>
import { ref, reactive, markRaw, shallowRef } from 'vue';
import CompA from './components/comp-a.vue';
import CompB from './components/comp-b.vue';
import CompC from './components/comp-c.vue';const tabsCurrent = ref(0);
const tabsList = ref([]);
const currentComp = shallowRef(CompA)const toggle = (index) => {tabsCurrent.value = index;switch (index) {case 1:currentComp.value = CompB;break;case 2:currentComp.value = CompC;break;default:currentComp.value = CompA;}
};
</script>
http://www.lryc.cn/news/189126.html

相关文章:

  • JVM 虚拟机面试知识脑图 初高级
  • PointRend: 将图像分割视为渲染——PointRend:Image Segmentation as Rendering
  • 【k8s】ingress-nginx通过header路由到不同后端
  • LuatOS-SOC接口文档(air780E)-- httpsrv - http服务端
  • Android Studio: unrecognized Attribute name MODULE
  • 云服务器带宽对上传下载速度的影响
  • 2023/9/28 -- ARM
  • vue原生实现element上传多张图片浏览删除
  • 黑群晖video station评级问题
  • Godot快速精通-从看懂英文文档开始-翻译插件
  • vue项目的学习周报03
  • ES中个别字段属性说明
  • Web前端-Vue2+Vue3基础入门到实战项目-Day3(生命周期, 案例-小黑记账清单, 工程化开发入门)
  • 如何在小程序首页设置标题栏文字
  • CPU性能分析--火焰图使用
  • 微服务10-Sentinel中的隔离和降级
  • python实现UI自动化配置谷歌浏览器驱动
  • AI如何帮助Salesforce从业者找工作?
  • 【Vue面试题十七】、你知道vue中key的原理吗?说说你对它的理解
  • 【数据结构】二叉树--堆排序
  • 项目log日志mysql记录,熟悉python的orm框架
  • 【数据结构-字符串 四】【字符串识别】字符串转为整数、比较版本号
  • React 组件传 children 的各种方案
  • 如何在一个传统的html中,引入vueJs并使用vue复制组件?
  • 【轻松玩转MacOS】故障排除篇
  • Linux基本指令(1)
  • 计算机毕业设计选题推荐-springboot 网上手机销售系统
  • 2、vscode c++ 项目配置调试及运行
  • 二叉搜索树的最近公共祖先
  • LuatOS-SOC接口文档(air780E)-- i2c - I2C操作