vue、uniapp实现组件动态切换

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

vue 中 component组件 is属性

功能描述

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

vue2版

html 复制代码
<template>
	<!-- 标签数据 -->
	<!-- uview-ui 标签组件 -->
	<u-tabs
		class="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'
					break
				case 2:
					currentCompName = 'CompC'
					break
				default:
					currentCompName = 'CompA'
			}
			return currentCompName
		},
	},

	methods: {
		toggle(index) {
			this.tabsCurrent = index
		},
	},
}
</script>

vue3版

html 复制代码
<template>
	<!-- 标签数据 -->
	<!-- uview-ui 标签组件 -->
	<u-tabs
		class="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>

或者

html 复制代码
<template>
	<!-- 标签数据 -->
	<!-- uview-ui 标签组件 -->
	<u-tabs
		class="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>
相关推荐
Y***9851几秒前
【学术会议论文投稿】Spring Boot实战:零基础打造你的Web应用新纪元
前端·spring boot·后端
q***333718 分钟前
SpringMVC新版本踩坑[已解决]
android·前端·后端
亿元程序员43 分钟前
做了十年游戏,我才意识到:程序员最该投资的,是一台专业的编程显示器
前端
IT_陈寒1 小时前
Python高手都在用的5个隐藏技巧,让你的代码效率提升50%
前端·人工智能·后端
源码技术栈1 小时前
什么是云门诊系统、云诊所系统?
java·vue.js·spring boot·源码·门诊·云门诊
lcc1871 小时前
Vue3 ref函数和reactive函数
前端·vue.js
艾小码1 小时前
还在为组件通信头疼?defineExpose让你彻底告别传值烦恼
前端·javascript·vue.js
gnip1 小时前
docker总结
前端
槁***耿1 小时前
TypeScript类型推断
前端·javascript·typescript
带只拖鞋去流浪1 小时前
迎接2026,重新认识Vue CLI (v5.x)
前端·vue.js·webpack