在使用框架iview的表单验证的时候报出错误

1 错误:TypeError: Cannot read property ‘validate’ of undefined

[Vue warn]: Error in v-on handler: "TypeError: Cannot read property 'validate' of undefined"

found in

---> <Button>
       <FormItem>
         <IForm>
           <Modal>

2 错误的源码:

<template>
    <Form :refs="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
        <FormItem label="Name" prop="name">
            <Input v-model="formValidate.name" placeholder="Enter your name"></Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">Submit</Button>
            <Button @click="handleReset('formValidate')" style="margin-left: 8px">Reset</Button>
        </FormItem>
    </Form>
</template>
<script>
    export default {
        data () {
            return {
                formValidate: {
                    name: '',
                },
                ruleValidate: {
                    name: [
                        { required: true, message: 'The name cannot be empty', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$Message.success('Success!');
                    } else {
                        this.$Message.error('Fail!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            }
        }
    }
</script>

3原因:

本代码的原因就是因为一个冒号引起的。
如图所示,就是因为 :refs="formValidate"

在这里插入图片描述

4 解决方法

:refs="formValidate"  改为    ref="formValidate"   即可

希望帮到你们

Logo

前往低代码交流专区

更多推荐