但此导入返回一个错误:Uncaught :意外标识符
如何将其导入我的vue.js组件?
首先,我正在使用webpack,并调用First Vuelidate
/**
* 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中进行导入,它总是给我带来错误。
这是我的组件的代码:
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)
}
}
},
});发布于 2019-03-13 08:10:18
您得到的错误是由于没有正确导入您想要使用的内容。导入这个库有几种方法。
全球进口和使用:
import Vue from 'vue'
import Vuelidate from 'vuelidate'
Vue.use(Vuelidate)或者,直接将混合器导入组件:
import { validationMixin } from 'vuelidate'
var Component = Vue.extend({
mixins: [validationMixin],
validations: { ... }
})或
import { required, maxLength } from 'vuelidate/lib/validators'或单独导入以减少进口大小:
import required from 'vuelidate/lib/validators/required'
import maxLength from 'vuelidate/lib/validators/maxLength'您也可以使用require
const { required, minLength } = require('vuelidate/lib/validators')对于用例,validators不存在于'vuelidate/lib/validators'中,因为它不是属性/成员。
更新:从您提供的Bootstrap示例链接获得的,可能是更旧版本的version。
Vuelidate不提供默认导出,因此此导入样式import validators from 'vuelidate/lib/validators'无法工作。另一方面,使用命名导入可以正常工作。
发布于 2019-03-13 07:45:37
您首先必须将其添加到应用程序中,如下所示:
import Vue from 'vue'
import Vuelidate from 'vuelidate'
Vue.use(Vuelidate)然后,您可以通过破坏来使用它,如下所示:
import { validators } from 'vuelidate'发布于 2019-03-13 07:49:19
ES6
使用命名导入并将验证部分更改如下:
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)
}
}
},
});https://stackoverflow.com/questions/55136623
复制相似问题