美文网首页
仿照element-ui 写vue通用验证组件

仿照element-ui 写vue通用验证组件

作者: 单只蝴蝶_569d | 来源:发表于2019-10-15 14:31 被阅读0次

最近为了练习vue,仿照elemeng-ui写了一个通用验证组件,先上element-ui的使用代码

<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="年龄" prop="age">
                <el-input v-model.number="ruleForm.age"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>

    export default {
        components: {KInput, KFormItem,KForm},
        data() {
            return {
                    ruleForm: {
                    pass: '111111',
                    age: ''
                },
                rules: {
                    pass: [
                        {required: true, trigger: 'blur', message: '请输入密码'},
                        {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ],
                    age: [
                        {required: true, message: '必填', trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            changeValue(argument) {
                this.ruleForm.pass = argument
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }

根据element-ui可以看出来,一共有三层,form formItem input。

1、input

实现Input

  • 任务1:实现自定义组件双向绑定功能
    v-model是语法糖,实现自定义组件双绑需要指定:value和@input即可
  • 任务2:值发生变化能够通知FormItem组件
<template>
    <label>
        <input type="text" v-model="ivalue" @input="onInput"/>
    </label>
</template>

<script>
    export default {
        name: "KInput",
        props: ['value'],
        data: function() {
            return {
                ivalue: this.value
            }
        },
        mounted() {
            //this.ivalue = this.value;
        },
        methods: {
            onInput(e) {
                this.$emit('changeValue', e.target.value)
                this.$parent.$emit('validate')
            }
        }
    }
</script>

<style scoped>

</style>

formItem的使用

  • 任务1:给Input预留插槽 - slot
  • 任务2:能够展示label和校验信息
  • 任务3:能够进行校验
    <div>
        <label>
            {{label}}
        </label>
        <slot></slot>
        <p v-if="errStatus">{{errorInfo}}</p>
    </div>

</template>

<script>
    import Schema from 'async-validator';
    export default {
        name: "KFormItem",
        props: [
            "label",
            "prop"
        ],
        inject:['kForm'],
        data: () => {
            return {
                errStatus: false,
                errorInfo: '',

            }
        },
        mounted(){
           this.$on('validate',this.validator)
        },
        methods: {
            validator(){
                console.log(this)
              //有两个input!  一个用户名 一个密码
                const rules = this.kForm.rules[this.prop];
                const value = this.kForm.model[this.prop];
                // 描述对象
                const descriptor = {[this.prop]:rules};
                const schema = new Schema(descriptor);
                schema.validate({[this.prop]:value},errors =>{
                    if(errors){
                        this.errMessage = errors[0].message;
                        this.errStatus = true;
                    } else {
                        this.errMessage = '';
                        this.errStatus = '';
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>

form的组件

  • 给form-item预留槽位
  • 将数据传递给后代便于它们访问数据模型和校验规则
<template>
    <div>
        <slot></slot>
    </div>
</template>

<script>
    export default {
        provide(){
            return {
                kForm: this
            }
        },
        props: {
            model: {
                type: Object,
                required: true,
            },
            rules: {
                type: Object
            }
        }
    }
</script>

<style scoped>

</style>
 

相关文章

网友评论

      本文标题:仿照element-ui 写vue通用验证组件

      本文链接:https://www.haomeiwen.com/subject/boajmctx.html