我正在尝试将Vuelidate插件库导入到newsletter.vue.js组件中。
但是此导入返回错误:Uncaught SyntaxError:意外的标识符
如何将其导入到vue.js组件中?
首先,我正在使用webpack并首先致电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');
}
Run Code Online (Sandbox Code Playgroud)
然后,我看到必须将“ 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)
}
}
},
});
Run Code Online (Sandbox Code Playgroud)
您首先必须将其添加到您的应用程序中,如下所示:
import Vue from 'vue'
import Vuelidate from 'vuelidate'
Vue.use(Vuelidate)
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过解构来使用它,如下所示:
import { validators } from 'vuelidate'
Run Code Online (Sandbox Code Playgroud)