向 vee 验证器添加自定义错误(ErrorBag)

Juj*_*bes 4 vue.js vuejs2 vee-validate

是否可以将自定义错误添加到 ErrorBag 中

我正在使用 nuxtjs。我已通过 nuxt.config.js 将 vee-validate 注册到我的插件中

效果很好但是

我想在模板中使用相同的错误代码

前任:

 <template>
   <div v-if="errors.all().length>0">
       //loop through
   </div>
 </template>
Run Code Online (Sandbox Code Playgroud)

我正在使用 axios 来获取用户信息。如果请求没有返回我预期的数据集。我想我可以简单地

 this.errors.push('this is my error message') //-> or some variant of this
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我发现 this.errors.push 不是一个函数

我知道

this.errors = ErrorBag{ __ob__: Observer} //-> has items and a vmId attributes
Run Code Online (Sandbox Code Playgroud)

如果我修改代码以推送到 ErrorBag 上,我会收到未定义的推送

Sea*_*ays 7

它记录在 ErrorBag 的 API 中。您可以添加自定义消息,例如:

// For example, you may want to add an error related to authentication:
errors.add({
  field: 'auth',
  msg: 'Wrong Credentials'
});
Run Code Online (Sandbox Code Playgroud)

检查此处的文档以获取更多信息:https://vee-validate.logaretm.com/v2/api/errorbag ​​.html