vue建立组件无校验版

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

1、非组件代码:

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

			&: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);

			&: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>

效果:
在这里插入图片描述

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zttbee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值