本文介绍: 有两个要传递的参数选项tabTitle,激活的选项索引activeIndex.还有一个要传递的事件handleChange,该事件有一个参数index组件文件src/components/TabBlock/index.vue

实现功能
切换,相当于tab
在这里插入图片描述

1、非组件代码

<template>
	<div&gt;
		<div class="tabStyle"&gt;
			<div v-for="(item,index) in tabTitle" :key="index" class="bordItemStyle" :class="choseIndex===index?'itemActiveStyle':'itemStyle'" @click="handleChose(index)"&gt;
				{{item}}
			</div&gt;
		</div>
	</div>
</template>

<script>
	export default {
		name: "home",
		data() {
			return {
				choseIndex: 0,
				tabTitle:['近7天','近15天','近30天'],
			}
		},

		mounted() {

		},
		methods: {
			handleChose(index) {
				this.choseIndex = index;
			},

		}
	}
</script>

<style lang="scss" scoped>
	.tabStyle {
		display: flex;

		.itemStyle {
			background-color: #ffffff;
		}

		.itemActiveStyle {
			color: #ffffff;
			background-color: #025DF4;
		}

		.itemStyle,
		.itemActiveStyle {
			padding: 8px 20px;
			cursor: pointer;
		}

		.bordItemStyle {
			border-top: 1px solid rgb(220, 223, 230);
			border-bottom: 1px solid rgb(220, 223, 230);
			border-left: 1px solid rgb(220, 223, 230);

			&amp;:last-child {
				border-right: 1px solid rgb(220, 223, 230);
			}
		}
	}
</style>


2、转换为组件代码
组件:文件src/components/TabBlock/index.vue
有两个要传递的参数选项名tabTitle,激活的选项索引activeIndex.还有一个要传递的事件handleChange,该事件有一个参数index

<template>
	<div>
		<div class="tabStyle">
			<div v-for="(item,index) in tabTitle" :key="index" class="bordItemStyle" :class="activeIndex===index?'itemActiveStyle':'itemStyle'" @click="$emit('handleChange',index)">
				{{item}}
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		name: "TabBlock",
		props: {
			tabTitle: {
				type: Array,
				default: ''
			},
			activeIndex: {
				type: Number,
				default: ''
			},
		},
		data() {
			return {
			}
		},

		mounted() {

		},
		methods: {

		}
	}
</script>

<style lang="scss" scoped>
	.tabStyle {
		display: flex;

		.itemStyle {
			background-color: #ffffff;
		}

		.itemActiveStyle {
			color: #ffffff;
			background-color: #025DF4;
		}

		.itemStyle,
		.itemActiveStyle {
			padding: 8px 20px;
			cursor: pointer;
		}

		.bordItemStyle {
			border-top: 1px solid rgb(220, 223, 230);
			border-bottom: 1px solid rgb(220, 223, 230);
			border-left: 1px solid rgb(220, 223, 230);

			&amp;:last-child {
				border-right: 1px solid rgb(220, 223, 230);
			}
		}
	}
</style>

调用组件的文件

<template>
	<div>
		<TabBlock :tabTitle="titles" :activeIndex="choseIndex" @handleChange="handleChose"></TabBlock>
	</div>
</template>

<script>
	import TabBlock from '@/components/TabBlock/index.vue'
	export default {
		name: "home",
		components:{
			TabBlock
		},
		data() {
			return {
				titles:['近7天','近15天','近30天'],
				choseIndex:0,
			}
		},

		mounted() {

		},
		methods: {
			handleChose(index){
				this.choseIndex = index;
				console.log("选中的索引",this.choseIndex)
				console.log("选中的名称",this.titles[this.choseIndex])
			}

		}
	}
</script>

<style lang="scss" scoped>
	
</style>

效果
在这里插入图片描述

原文地址:https://blog.csdn.net/qq_43840793/article/details/134650066

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_7709.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注