In my webpack.mix.js
I have something like this:
// vue components
mix.js('resources/assets/js/app.js', 'public/js');
// scripts which I need
mix.scripts([
'resources/assets/js/jquery-2.2.4.min.js',
'resources/assets/js/bootstrap.min.js',
'resources/assets/js/fastclick.js',
'resources/assets/js/toastr.min.js',
'resources/assets/js/select2.min.js',
...
], 'public/assets/js/scripts.js');
// styles which I need
mix.styles([
"resources/assets/css/bootstrap-colorpicker.min.css",
"resources/assets/css/bootstrap-select.min.css",
"resources/assets/css/bootstrap.min.css",
"resources/assets/css/select2.min.css",
...
], 'public/assets/css/styles.css');
And I can initialize, for example, a select2
outside my vue components.
But when I trying to use this plugin (or any script of my mix.scripts
block) I get the next error:
$(...).select2 is not a function
Okey, I search a little and I found a "solution", before the export default{}
of my vue component I need to require the select2
script:
...
import Vue from 'vue';
require('../select2.min.js');
export default{
...
And now it works inside and outside of my vue component: BUT I "import" my select2 plugin twice:
- Inside my
public/assets/js/scripts.js
(which I need, because I use select2 and other plugins without a vue component) - And inside my
resources/assets/js/app.js
(when I runnpm run watch
ornpm run production
the compiler adds the requirement of my vue component to this script file)
So, my question is: How can I make my components in VUE use the JQuery scripts coming from my public/assets/js/scripts.js
file instead of having to require each of them within each vue component?
I hope there is a way to do that