Iview的Tab栏校验不通过不跳转

本文介绍了如何在Iview的Tab组件中集成Form表单进行校验,确保提交前必填字段不为空。展示了代码实现,包括<FormItem>的验证规则和异步处理,以及如何根据校验结果控制Tab之间的跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Iview的Tab栏有校验,校验不通过不跳转

效果图

在这里插入图片描述

代码实现

<template>
    <Tabs v-model="name" @on-click="handle">
       
        <TabPane label="label1" name="basic">
          <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
            
      
            <FormItem label="Name" prop="name1">
            <Input v-model="formValidate.name1" placeholder="Enter your name1"></Input>
        </FormItem>
                <Button type="primary" @click="handleSubmit('formValidate')">Submit</Button>
                <Button @click="handleReset('formValidate')" style="margin-left: 8px">Reset</Button>

        </Form>
        </TabPane>
        <TabPane label="label2" name="search">
         这里是Tab2内容
        </TabPane>
    </Tabs>
</template>
<script>
    export default {
         data(){
         return{
           flag:true,
           name:'basic',
           temp:'basic',
             formValidate: {
                    name1: '',
                },
                ruleValidate: {
                    name1: [
                        { required: true, message: 'The name cannot be empty', trigger: 'blur' }],
                }
         }
       },
         methods:{
	       async handle(a){
	         await this.handleSubmit() //这里必须使用await
	         if(this.flag==true){
	               this.temp=a
	               this.name=this.temp
	         }else{
	              this.name=this.temp
	         }
	       },
	        handleSubmit () {
	          if(this.temp=='basic'){
	             this.$refs['formValidate'].validate((valid) => {
	                if (valid) {
	                  this.flag=true
	                  return 
	                } else {
	                  this.flag=false
	                  return 
	                 }
	              })
	          }
	        },
	        handleReset (name) {
	          this.$refs[name].resetFields();
	        }
     },
    }
  
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值