1 <template> 2 <Form ref="formInline" :model="formInline" :rules="ruleInline" inline> 3 <Form-item prop="user"> 4 <Input type="text" v-model="formInline.user" placeholder="Username"> 5 <Icon type="ios-person-outline" slot="prepend"></Icon> 6 </Input> 7 </Form-item> 8 <Form-item prop="password"> 9 <Input type="password" v-model="formInline.password" placeholder="Password"> 10 <Icon type="ios-locked-outline" slot="prepend"></Icon> 11 </Input> 12 </Form-item> 13 <Form-item> 14 <Button type="primary" @click="handleSubmit('formInline')">登录</Button> 15 </Form-item> 16 </Form> 17 </template> 18 <script> 19 export default { 20 data () { 21 return { 22 formInline: { 23 user: '', 24 password: '' 25 }, 26 ruleInline: { 27 user: [ 28 { required: true, message: '请填写用户名', trigger: 'blur' } 29 ], 30 password: [ 31 { required: true, message: '请填写密码', trigger: 'blur' }, 32 { type: 'string', min: 6, message: '密码长度不能小于6位', trigger: 'blur' } 33 ] 34 } 35 } 36 }, 37 methods: { 38 handleSubmit(name) { 39 this.$refs[name].validate((valid) => { 40 if (valid) { 41 this.$Message.success('提交成功!'); 42 } else { 43 this.$Message.error('表单验证失败!'); 44 } 45 }) 46 } 47 } 48 } 49 </script>
使用:prop
传递数据格式为数字、布尔值或函数时,必须带:
在<Form>标签中的ref属性是vue中的ref属性,被用来给元素或子组件引用信息,引用信息将会被注册在父组件的 $refs
对象上,主要是用来方便定位到该组件;:model属性绑定的是一个对象,该对象保存着后面表单项所需要的数据。同时,<Form-item>中的prop属性绑定的是表单项的数据,将其与表单项关联起来,在表单验证中很重要