vue.js - 将自定义错误添加到vee验证器(ErrorBag)

标签 vue.js vuejs2 vee-validate

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

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

它工作正常但是

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

例如:

 <template>
   <div v-if="errors.all().length>0">
       //loop through
   </div>
 </template>

我正在使用axios来获取用户信息。
如果请求未返回我的预期数据集。我以为我可以
 this.errors.push('this is my error message') //-> or some variant of this

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

我知道
this.errors = ErrorBag{ __ob__: Observer} //-> has items and a vmId attributes

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

最佳答案

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

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

在此处查看文档以获取更多信息:https://baianat.github.io/vee-validate/api/errorbag.html#api

关于vue.js - 将自定义错误添加到vee验证器(ErrorBag),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55385134/

相关文章:

javascript - Vue router-link 精确激活类

vue.js - vue路由器无法删除查询

javascript - Vue.js 计时计算与纯 JavaScript 版本不匹配

javascript - Vue.js 2-将表单分解成更小的组件是一种不好的做法吗

vue.js - 无法使用 VueSelect 让 VeeValidate 3.x 与自定义组件一起使用

javascript - 如何将 VeeValidate 与 vue-i18n 集成?

unit-testing - Vue.js 单元 :test : Is there any way to mock a Vee-Validate validator plugin?

javascript - 将动态 bool 属性传递给 VueJS 组件

javascript - 如何在 vue js html 中将到期时间显示为报价的计时器?

vue.js - axios post请求从catch中获取主体?