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
    评论
Vue中,可以通过在父组件中调用子组件的方法来校验组件的form表单。首先,在父页面的模板中引入子组件,并给子组件添加一个ref属性,例如`<children ref="children"/>`。然后,在父组件的方法中,使用`this.$refs['children']`来获取子组件的实例,从而可以调用子组件的方法。具体的实现代码如下所示: ``` <template> <div class="hello"> <el-button type="primary" @click="submitHandle">保存</el-button> <!-- 子组件的引入 --> <children ref="children"/> </div> </template> <script> import children from './test' export default { name: "test", components:{ children }, data() { return { form: { name: "" }, rules: {}, }; }, methods: { submitHandle () { // 调用子组件中的validateHandle校验方法 const result = this.$refs['children'].validateHandle() if (result) { console.log('子组件校验成功') } else { console.log('子组件校验失败') } } }, }; </script> ``` 在上述代码中,父组件调用了子组件的`validateHandle`方法来进行校验,`validateHandle`方法在子组件中的实现可以根据具体需求来编写。通过这种方式,父组件可以校验组件中的form表单。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue form表单父页面对子组件进行校验](https://blog.csdn.net/qq_42323925/article/details/109582405)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue动态绑定组件子父组件多表单验证功能的实现代码](https://download.csdn.net/download/weixin_38675967/12759867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zttbee

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

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

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

打赏作者

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

抵扣说明:

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

余额充值