1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| <template> <div> <a-form :form="myLogin" @submit="handleSubmit"> <a-form-item label="项目">
<a-input v-decorator="['name']" /> </a-form-item> <a-form-item>
<a-input v-decorator="['age', rules.age]" /> </a-form-item> <a-form-item> <a-input v-decorator="['adddess', rules.adddess]" /> </a-form-item> <a-form-item>
<a-input v-decorator="['sex', rules.sex]" /> </a-form-item>
<a-form-item> <a-button html-type="submit"> 提交校验所有值:this.myLogin.validateFields((err, values) </a-button> <br> <a-button @click="subOneVal"> 提交校验某个值:this.myLogin.validateFields(["age"], (err, fieldsValue) 并返回这个值 </a-button> <br> <a-button @click="btnSetVal"> 设置值/一个/多个: this.myLogin.setFieldsValue({ name: "名字", age: "28" }); </a-button> <br> <a-button @click="getOneVal"> 获取一个值: this.myLogin.getFieldsValue(["name"]); </a-button> <br> <a-button @click="getMoreVal"> 获取多个值: this.myLogin.getFieldsValue(["name", "age"]); </a-button> <br> <a-button @click="getAllVal"> 获取全部值/表单提交: this.myLogin.getFieldsValue(); </a-button> <br> <a-button @click="reset"> 重置表单:this.form.resetFields(); <br> 默认值重置无效,可以赋值为空解决 </a-button> </a-form-item> </a-form> </div> </template>
<script> export default { name: "Test", data() { return { myLogin: this.$form.createForm(this, { name: 'newForm' }), rules: { age: { rules: [{ required: true, message: "年龄不能为空" }] }, adddess: { rules: [{ required: true, message: "地址不能为空" }] }, sex: { rules: [{ required: true, message: "不能为空" }], initialValue: "男" } } }; }, methods: { handleSubmit(e) { e.preventDefault(); this.myLogin.validateFields((err, values) => { if (!err) { console.log("Received values of form: ", values); } }); }, subOneVal() { this.myLogin.validateFields(["age"], (err, fieldsValue) => { if (!err) { console.log(fieldsValue); } else { console.log(err); } }); }, btnSetVal() { this.myLogin.setFieldsValue({ name: "名字", age: "28" }); }, getOneVal() { this.myLogin.getFieldsValue(["name"]); console.log(this.myLogin.getFieldsValue(["name"])); }, getMoreVal() { this.myLogin.getFieldsValue(["name", "age"]); console.log(this.myLogin.getFieldsValue(["name", "age"])); }, getAllVal() { this.myLogin.getFieldsValue(); console.log(this.myLogin.getFieldsValue()); }, reset() { this.myLogin.resetFields(); } } }; </script> <style lang="" scoped>
</style>
|