下面代码中,按钮上绑定的test方法会被触发两次:一次时vue初始化时渲染时会调用一次,另一次是当我在mounted生命周期钩子函数中调用Ant Design Vue中表单的validateFields方法时会再次触发按钮上的test方法。
不能理解为什么调用了validateFields会再次触发按钮上绑定的test方法,这是什么原理呢?
<template>
<div class="layout-box">
<a-form :form="accountForm" :label-col="{ span: 24 }" :wrapper-col="{ span: 24 }">
<a-form-item :label="$t('lang.login.account')">
<a-input
:placeholder="$t('lang.login.accountPlaceholder')"
v-decorator="['account', { rules: rules.account }]"
></a-input>
</a-form-item>
<a-form-item :label="$t('lang.login.password')">
<a-input-password
:placeholder="$t('lang.login.pwdPlaceholder')"
v-decorator="['password', { rules: rules.password }]"
></a-input-password>
</a-form-item>
</a-form>
<a-button class="form-btn" type="primary" :disabled="test()">
{{ $t('lang.login.login') }}
</a-button>
</div>
</template>
<script>
import { test } from '@/utils/util'
export default {
name: 'Login',
data() {
return {
test,
accountForm: this.$form.createForm(this, { name: 'login' }),
rules: {
account: [{ required: true, message: this.$t('lang.login.accountPlaceholder') }],
password: [{ required: true, message: this.$t('lang.login.pwdPlaceholder') }],
},
}
},
mounted() {
this.accountForm.validateFields()
},
}
</script>