我在一个表单中有多个输入,通过vee-validate验证,它工作得很好,但我只想检查事件上的一些字段。所以在看到关于这个https://github.com/baianat/vee-validate/issues/1089的一个问题后,我写了这个函数:
async checkInputs(){
let inputs = ['input1', 'input2', 'input3', 'input4'];
const results = Promise.all(
inputs.map(input => {
if(this.$validator.validate(input) === true)
null;
else
dictionary.custom[input].required();
})
);
return (await results).filter(element => element != null);
}其思想是获得一个包含失败字段的错误消息的数组。注意,我使用了一些console.log来确保当输入检查失败时,它会返回错误消息。但是当我使用它的时候:
this.checkInputs().then(function(results) {
console.log(results);
});我只有一个包含4个未定义值的数组,而不是包含4个字符串的错误消息数组。
发布于 2019-03-31 21:44:43
你试过$nextTick吗?
this.$nextTick(function() {
this.checkInputs().then(function(results) {
console.log(results);
});
});发布于 2019-10-14 02:55:54
Array.map()应该返回值,this.$validator.validate(input)返回值也应该是promise,所以你可以这样写:
inputs.map(async input => {
if(await this.$validator.validate(input) === true)
return null;
else
return dictionary.custom[input].required();
})https://stackoverflow.com/questions/52986745
复制相似问题