1、视图模板
<a-form
id="formLogin"
ref="formLogin"
:form="form"
:layout="formLayout"
@submit="handleSubmit"
>
<a-form-item>
<a-input
size="large"
type="text"
placeholder="请输入用户名"
v-decorator="[
'username',
{rules: [{ required: true, message: '请输入用户名' }], validateTrigger: 'change'}
]"
>
<a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
</a-input>
</a-form-item>
<a-form-item>
<a-input
size="large"
type="password"
autocomplete="false"
placeholder="请输入密码"
v-decorator="[
'password',
{rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur'}
]"
>
<a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
</a-input>
</a-form-item>
<a-form-item style="margin-top:24px">
<a-button
size="large"
type="primary"
htmlType="submit"
class="login-button"
>确定</a-button>
</a-form-item>
</a-form>
2、JS脚本
<script>
export default {
name: 'LoginForm',
data () {
return {
form: this.$form.createForm(this), // 创建form,只要有form组件必须调用该方法
formLayout: 'vertical' // 表单布局,可以是horizontal、vertical或inline任意一种
}
},
methods: {
// handler
handleSubmit (e) {
e.preventDefault()
this.form.validateFields((err, values) => { // 表单校验
if (!err) {
console.log('Received values of form: ', values)
}
})
}
}
}
</script>
3、CSS样式
<style lang="less" scoped>
.login-button {
display: block;
margin: 15px 10px;
}
</style>