0

Im using require to include Javascript plugins for my project, but for some reason, the plugins cant be found by the browser. Starting npm run dev will compile all the scripts I need(scripts also located in the compiled file), but when running the project in the browser, it simply wont work.

[Vue warn]: Error in mounted hook: "TypeError: $(...).datepicker is not a function"

Here my files:

app.js

require('./bootstrap');

window.Vue = require('vue');

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

new Vue({
  el: '#app',
  mounted: function() {
      $( "#datepicker" ).datepicker();
  }
});

bootstrap.js

window._ = require('lodash');


try {
    window.$ = window.jQuery = require('jquery');
    require('bootstrap');

    window.datepicker = require('jquery-datepicker');

} catch (e) {}


window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

webpack.mix.js

let mix = require('laravel-mix');

mix.js('resources/assets/js/app.js', 'public/js')
      .sass('resources/assets/sass/app.scss', 'public/css');

Can anybody help me?

1 Answer 1

2

Finally found a solution by myself by following these instructions..

https://github.com/JeffreyWay/laravel-mix/blob/master/docs/jquery-ui.md

You have to do it like this in your app.js

import $ from 'jquery';
window.$ = window.jQuery = $;

import 'jquery-ui/ui/widgets/datepicker.js';

Now you can call the libray in your prefered way.

I guess this should work for other JS librays too..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.