K
K
Konstantin Eliseev2019-01-17 09:50:01
webpack
Konstantin Eliseev, 2019-01-17 09:50:01

Am I including jquery libraries in Laravel mix correctly?

app.js is loaded on all pages
, and create.js or profile.js are additionally loaded only on those that I need.
1) Tell me if I'm using mix correctly?
2) Doesn't the line import $ from 'jquery' cause jquery to be included a second time?
app.js profile.js
window.$ = window.jQuery = require('jquery');

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

require('jquery-mask-plugin');

$(document).ready(function(){
    $('#phone').mask('+9 (999) 999-9999');
});

create.js
import $ from 'jquery';
window.$ = window.jQuery = $;
window.repeatable = require("jquery.repeatable/jquery.repeatable.js");

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

$('#expires_at').datepicker();

$(function() {
  $(".form-account-warehouses-create .repeatable").repeatable();
});

webpack.mix.js
mix
    .setPublicPath('public/build')
    .setResourceRoot('/build/')
    .js('resources/js/app.js', 'js')
    .js('resources/js/profile.js', 'js')
    .js('resources/js/create.js', 'js')
    .version();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Tratorov, 2019-03-01
@K0r5hun

A working version of connecting jQuery in your js file, which is compiled through mix.js, will be like this:

global.jquery = global.jQuery = global.$ = require('jquery');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question