Going deep into the science, we must create their own wheels, then we achieve the realization of Element-ui in the form of a manual form
1, using the form component
<template>
<div class="form-index">
<k-form :model="ruleForm" :rules="rules" ref="form">
<k-form-item label="用户名" prop="username">
<k-input v-model="ruleForm.username" placeholder="请输入用户名"></k-input>
</k-form-item>
<k-form-item label="密码" prop="password">
<k-input v-model="ruleForm.password" type="password" placeholder="请输入密码"></k-input>
</k-form-item>
<k-form-item>
<button @click="submitForm">login</button>
</k-form-item>
</k-form>
</div>
</template>
<script>
import KForm from './KForm'
import KFormItem from './KFormItem'
import KInput from './KInput'
export default {
name: 'kFormIndex',
data () {
return {
ruleForm: {
username: '',
password: ''
},
rules: {
username: [
{ required: true, message: '请输入用户名', trigger: 'blur' }
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' }
]
}
}
},
components: {
KForm,
KFormItem,
KInput
},
methods: {
submitForm () {
this.$refs.form.validate(result => {
if (result) {
alert('success')
} else {
alert('failure')
}
})
}
}
}
</script>
By the above code, we can see that, using a form assembly is a three-layer structure. And verifies the rule data rules
and form data model
in an outermost layer k-form
on the form, because of the k-form-item
need to use rules
and model
defined in the outermost component, subassembly easy to use
the relationship between the figure can clearly display assembly, and, through slot
the slot to build relationships between components, then we look to achieve the various components
k-input component
<template>
<div>
<input v-bind="$attrs" :value="value" @input="inputHandle">
</div>
</template>
<script>
export default {
inheritAttrs: false, //默认继承的属性去掉
name: 'kInput',
props: {
value: {
type: String
}
},
methods: {
inputHandle (e) {
this.$emit('input', e.target.value)
// 分发检验事件
this.$parent.$emit('validate')
}
}
}
</script>
<style lang="scss" scoped>
</style>
- By the effect of form component, you can see
k-input
two-way data binding implementation. Used in the outer layerv-model
is actually a syntactic sugar (to bindvalue
and listen forinput
events), ink-input
assembly, we need to acceptprop
and distributeinput
events to implement two-way data binding. $attrs
Can get the value from the parent component pass over, and expand on the current label.this.$parent.$emit('validate')
Eachinput
value input box is changed need to be verified, since the parent components are used inslot
the slot, the use within parent componentsthis.$on('validate', this.validate)
for monitoring.
k-form-item
<template>
<div>
<span v-if="label">{{ label }}</span>
<slot></slot>
<p v-if="errMessage">{{errMessage}}</p>
</div>
</template>
<script>
import Schema from 'async-validator'
export default {
name: 'kFormItem',
inject: ['form'],
props: {
label: {
type: String,
default: ''
},
prop: {
type: String
}
},
data () {
return {
errMessage: ''
}
},
mounted () {
this.$on('validate', () => { this.validate() })
},
methods: {
validate () {
// 做校验
const value = this.form.model[this.prop]
const rules = this.form.rules[this.prop]
const desc = { [this.prop]: rules }
const schema = new Schema(desc)
return schema.validate({ [this.prop]: value }, error => {
if (error) {
this.errMessage = error[0].message
} else {
this.errMessage = ''
}
})
}
}
}
</script>
<style lang="scss" scoped>
</style>
In k-form-item
will be form validation and error message display component. But model
and rules
are blocked in the k-form
assembly, I used here provide
and inject
were passaged value between components in k-form-item
can get validation rules component using async-validator check the library for verification.
k-form
<template>
<div>
<slot></slot>
</div>
</template>
<script>
export default {
name: 'kForm',
provide () {
return {
form: this
}
},
props: {
model: {
type: Object
},
rules: {
type: Object
}
},
methods: {
validate (cb) {
const tasks = this.$children
.filter(item => item.prop)
.map(item => item.validate())
Promise.all(tasks)
.then(() => cb(new Error(true)))
.catch(() => {
const flag = false
cb(flag)
})
}
}
}
</script>
<style lang="scss" scoped></style>
k-form
Form provides a validate
method of checking whether it is through the check form. Using k-form
the form, Register or Log in judgment when it entry form is correct
Renderings
to sum up
- Use of technology to the point
- Between components by value
props
, Parent component subassembly to pass through properties$emit
, Sub-assemblies through the distribution of events by value, parent component to monitor received value, it can also be usedthis.$on
andthis.$parent.$emit
provide
Andinject
communication between the ancestor and descendant component assembly
slot
Reference slotv-model
Two-way data binding implementationPromise.all
usage ofasync-validator
Using a checksum library
- Between components by value
- Component design, first try to use this to write a shelf components, then the communication connection between the components of the various components of how you use components individually to implement the various components, a clear function of the individual components by