首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导入vue.js组件

导入vue.js组件
EN

Stack Overflow用户
提问于 2019-03-13 07:38:15
回答 5查看 1.7K关注 0票数 5

我正在尝试将一个武埃林插件库导入到我的武埃林组件中。

但此导入返回一个错误:Uncaught :意外标识符

如何将其导入我的vue.js组件?

首先,我正在使用webpack,并调用First Vuelidate

代码语言:javascript
复制
/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');


window.Vue = require('vue');

import BootstrapVue from 'bootstrap-vue'
import Vuelidate from 'vuelidate'

Vue.use(BootstrapVue)
Vue.use(Vuelidate)

/**
 * The following block of code may be used to automatically register your
 * Vue components. It will recursively scan this directory for the Vue
 * components and automatically register them with their "basename".
 *
 * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
 */

// const files = require.context('./', true, /\.vue$/i)
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))

//Vue.component('example-component', require('./components/ExampleComponent.vue').default);

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

const app = new Vue({
});

window.onload = function(){
    app.$mount('#app');
}

然后,我发现为了使用它,必须将'vuelidate/lib/validators‘导入到组件中。

就像这个例子

问题是我无法在组件vue中进行导入,它总是给我带来错误。

这是我的组件的代码:

代码语言:javascript
复制
import validators from 'vuelidate/lib/validators';//this return me error

Vue.component('newsletter', {

    template :  '<div>\
      <b-form @submit="onSubmit">\
        \
          \
        <b-form-group id="exampleInputGroup2" label="Food" label-for="exampleInput2">\
          <b-form-select\
            id="exampleInput2"\
            :options="foods"\
            :state="$v.form.food.$dirty ? !$v.name.$error : null"\
            v-model="form.food"\
          />\
  \
          <b-form-invalid-feedback id="input2LiveFeedback">\
            This is a required field\
          </b-form-invalid-feedback>\
        </b-form-group>\
  \
        <b-button type="submit" variant="primary" :disabled="$v.form.$invalid">Submit</b-button>\
      </b-form>\
    </div>',

    props : ['route_post'],

    data: function()
    {
        return {
            foods: ['apple', 'orange'],
            form: {}
          }
    },  

    validations: {
      form: {
        name: {
          required: validators.required,
          minLength: validators.minLength(3)
        }
      }
    },

});
EN

回答 5

Stack Overflow用户

发布于 2019-03-13 08:10:18

您得到的错误是由于没有正确导入您想要使用的内容。导入这个库有几种方法。

全球进口和使用:

代码语言:javascript
复制
import Vue from 'vue'
import Vuelidate from 'vuelidate'
Vue.use(Vuelidate)

或者,直接将混合器导入组件:

代码语言:javascript
复制
import { validationMixin } from 'vuelidate'

var Component = Vue.extend({
  mixins: [validationMixin],
  validations: { ... }
})

代码语言:javascript
复制
import { required, maxLength } from 'vuelidate/lib/validators'

或单独导入以减少进口大小:

代码语言:javascript
复制
import required from 'vuelidate/lib/validators/required'
import maxLength from 'vuelidate/lib/validators/maxLength'

您也可以使用require

代码语言:javascript
复制
const { required, minLength } = require('vuelidate/lib/validators')

对于用例,validators不存在于'vuelidate/lib/validators'中,因为它不是属性/成员。

更新:从您提供的Bootstrap示例链接获得的,可能是更旧版本的version。

Vuelidate不提供默认导出,因此此导入样式import validators from 'vuelidate/lib/validators'无法工作。另一方面,使用命名导入可以正常工作。

票数 1
EN

Stack Overflow用户

发布于 2019-03-13 07:45:37

您首先必须将其添加到应用程序中,如下所示:

代码语言:javascript
复制
import Vue from 'vue'
import Vuelidate from 'vuelidate'
Vue.use(Vuelidate)

然后,您可以通过破坏来使用它,如下所示:

代码语言:javascript
复制
import { validators } from 'vuelidate'
票数 0
EN

Stack Overflow用户

发布于 2019-03-13 07:49:19

ES6

使用命名导入并将验证部分更改如下:

代码语言:javascript
复制
import { validators, minLength } from 'vuelidate/lib/validators';

Vue.component('newsletter', {

    template :  `<div>
      <b-form @submit="onSubmit">
        <b-form-group id="exampleInputGroup2" label="Food" label-for="exampleInput2">
          <b-form-select
            id="exampleInput2"
            :options="foods"
            :state="$v.form.food.$dirty ? !$v.name.$error : null"
            v-model="form.food"
          />
          <b-form-invalid-feedback id="input2LiveFeedback">
            This is a required field
          </b-form-invalid-feedback>
        </b-form-group>
        <b-button type="submit" variant="primary" :disabled="$v.form.$invalid">Submit</b-button>
      </b-form>
    </div>`,

    props : ['route_post'],

    data: function()
    {
        return {
            foods: ['apple', 'orange'],
            form: {}
          }
    },  

    validations: {
      form: {
        name: {
          required,
          minLength: minLength(3)
        }
      }
    },

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55136623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档