【问题标题】:VeeValidate errors is not defined when importing into custom plugin导入自定义插件时未定义 VeeValidate 错误
【发布时间】:2019-05-16 08:32:46
【问题描述】:

我正在使用 vue create CLI 构建自定义 Vue JS 插件。我已经导入了各种包,但是在尝试将自定义设置与 VeeValidate 一起使用时出现错误。

plugin.js

/*
 * NOTE:
 *   This file is plugin stub for main.js
 */

// Import required packages
import Vue from 'vue'
import VeeValidate from 'vee-validate'
import VueResource from 'vue-resource'
import BootstrapVue from 'bootstrap-vue'

// Import custom LES form styling
import './assets/scss/les-application.scss'

// Import our plugin
import plugin from './index'

/* Vue Validation */
const config = {
  fieldsBagName: 'fields',
  errorBagName: 'errors',
  classes: true,
  strict: false,
  classNames: {
    valid: '',
    invalid: 'is-invalid'
  },
  events: 'change|blur',
  validity: false,
  inject: false,
  aria: true,
  delay: 0
}

Vue.use(require('vue-moment'))
Vue.use(VueResource)
Vue.use(VeeValidate, config)
Vue.use(plugin)

// Custom validation rules


/*
 * NOTE:
 *   If you want Vue instance of main.js to import something in your plugin as a Vue option,
 *   you need to export it here.
 */
// export default plugin

ma​​in.js

import Vue from 'vue'
import App from './App.vue'
import './plugin'

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')

错误:

Property or method "errors" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

Cannot read property 'has' of undefined

我正在尝试找出我的配置中缺少什么,如果我从 plugin.js 中删除 VeeValidate 配置,那么它会使用默认的 VeeValidate 选项并且可以正常工作。

【问题讨论】:

  • 实际使用 VeeValidate 的组件的代码在哪里?
  • 代码在/src/components/MyComponent.vue
  • 对对对,那我们能从MyComponent.vue看到相关代码吗?
  • @RyanHolton 我认为这是因为您禁用了自动注入。您需要在需要验证的每个组件中 request a new validator scope
  • @Sovalina 我将如何设置这个,我已经阅读了文档,但是我很困惑我的代码 sn-ps 会放在哪里

标签: vue.js vuejs2 vee-validate


【解决方案1】:

这是验证配置中的inject: false, 造成的。如果将此设置为 true 或不设置,则错误对象应该被注入到您的组件中而不会出现错误。

inject:false 在 vee-validate 2 http://vee-validate.logaretm.com/v2/concepts/injections.html 中停止自动注入

这会随着 vee-validate 3 而改变,其中需要使用 ValidationObserverValidationProvider https://logaretm.github.io/vee-validate/migration.html#migrating-from-2-x-to-3-0

【讨论】:

    猜你喜欢
    • 2019-09-28
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2020-01-03
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多