与Vue搭配的后台管理UI框架,最火的莫过于饿了吗的element-ui和阿里的Ant Design,这两个框架都在实际项目上使用过,也都是各有各的优势html
最早接触的仍是element - ui,后来项目调整,才接触到Ant Design,Form表单这块不支持双向绑定式的校验功能,在1.5.0+版本增长了FormModel表单才支持。今天就讲讲这两个在项目上使用的区别element-ui
1 Form 表单markdown
具备数据收集、校验和提交功能的表单,包含复选框、单选框、输入框、下拉选择框等元素。可是不支持双向绑定框架
使用ide
<template> <a-form :form="form"></a-form> </template> <script> export default { data() { return { form: this.$form.createForm(this, { name: 'coordinated' }), }; }, }; </script>
表单验证ui
<template> <a-form :form="form" @submit="handleSubmit"> <a-form-item> <a-button type="primary" html-type="submit"> Submit </a-button> </a-form-item> </a-form> </template> <script> export default { data() { return { form: this.$form.createForm(this, { name: 'coordinated' }), }; }, methods: { // 表单验证 handleSubmit(e) { e.preventDefault(); this.form.validateFields((err, values) => { if (!err) { console.log('Received values of form: ', values); } }); }, }, }; </script>
表单赋值this
this.form.setFieldsValue({ note: `Hi, ${value === 'male' ? 'man' : 'lady'}!`, });
赋值的时候须要调用setFieldsValue才能改变表单控件的值。双向绑定
2 FormModel 表单code
功能同上,支持v-modelorm
使用
<template> <a-form-model ref="ruleForm" :model="form"> <a-form-model-item> <a-input v-model="form.name"/> </a-form-model-item> </a-form-model> </template> <script> export default { data() { return { form: { name: '', }, }; }, }; </script>
表单验证
表单验证a-form-model增长rules,再每一个表单增长ref和prop就能够了,rules这个是对象,因此的验证规则均可以写在里面,一个表单项也能够有多个验证规则。
<template> <a-form-model ref="ruleForm" :model="form" :rules="rules"> <a-form-model-item ref="name" label="Activity name" prop="name"> <a-input v-model="form.name"/> </a-form-model-item> <a-form-model-item> <a-button type="primary" @click="onSubmit"> Create </a-button> </a-form-model-item> </a-form-model> </template> <script> export default { data() { return { form: { name: '', }, rules: { name: [ { required: true, message: 'Please input Activity name', trigger: 'blur' }, { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' }, ], }, }; }, methods: { onSubmit() { this.$refs.ruleForm.validate(valid => { if (valid) { alert('submit!'); } else { console.log('error submit!!'); return false; } }); }, }, }; </script>
表单赋值
表单赋值这个因为是双向绑定,因此直接赋值便可
this.form.name = 'lilei'
3 总结
Form和FormModel都具备数据收集、校验和提交功能,区别就在因而否支持双向绑定式的校验功能,若是使用FormModel,Ant Design版本须要1.5.0+。