这是我的 .vue 组件文件,其中我没有使用 Vue.use()
来安装 vee-validate
验证包。如文档中所述,该模块会将 errors
对象注入(inject) data
对象。
但我确实收到错误 _vm.errors is undefined
。我不确定我是否正确实例化了它。
另外,import Vue from 'Vue'
和 Vue.use(VeeValidate)
是否只为这个组件“使用”vee-validate?(这就是我想要的它来做)。
<template>
<input class="form-control" name="name" placeholder="Name" type="text" v-model="player.name" v-validate data-vv-rules="alpha|min:2|max:50" :class="{'input': true, 'is-danger': errors.has('name') }">
</template>
<script>
import Vue from 'Vue';
import VeeValidate from 'vee-validate';
Vue.use(VeeValidate);
export default {
data() {
return {
// errors: '',
showForm: false,
player: null
}
}
}
</script>
最佳答案
我遇到了类似的问题,但我使用的是这些版本:
“vee-验证”:“^2.0.0-rc.21”, "vue": "^2.5.2",
在这种情况下,您可以使用以下方法访问错误集合:this.$validator.error
查看 vee-validate 的规范:
http://vee-validate.logaretm.com/api.html#error-bag (404!)
新链接: http://vee-validate.logaretm.com/v2/api/errorbag.html
向下滚动,你会看到这个:
验证者
验证器作为 $validator 自动注入(inject)到 Vue 实例中。然而,它也是一个独立的类,可以单独用于以编程方式验证值。构造函数可以选择采用一个对象来将每个字段名称映射到一组验证。
关于vue-component - vee-validate 错误对象 - _vm.errors 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41162865/