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>
相关推荐
人工智能训练师7 小时前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js
Seveny077 小时前
pnpm相对于npm,yarn的优势
前端·npm·node.js
yddddddy8 小时前
css的基本知识
前端·css
昔人'8 小时前
css `lh`单位
前端·css
Nan_Shu_61410 小时前
Web前端面试题(2)
前端
知识分享小能手10 小时前
React学习教程,从入门到精通,React 组件核心语法知识点详解(类组件体系)(19)
前端·javascript·vue.js·学习·react.js·react·anti-design-vue
00后程序员张10 小时前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
蚂蚁RichLab前端团队11 小时前
🚀🚀🚀 RichLab - 花呗前端团队招贤纳士 - 【转岗/内推/社招】
前端·javascript·人工智能
孩子 你要相信光11 小时前
css之一个元素可以同时应用多个动画效果
前端·css
萌萌哒草头将军11 小时前
Oxc 和 Rolldown Q4 更新计划速览!🚀🚀🚀
javascript·vue.js·vite