"$ Is Not Defined" When Using $ In View After Requiring It In App.js
I have an app and want to use Jquery
in script tags inside my views.
I've tried moving the imports and making sure the app.js
with the require jquery
is loaded BEFORE the other script does (with alerts, one right after the importation, one right before the jquery I want to use).
I guess I could make one file per view and change my webpack
config and require each time jquery
, but that would be bothersome.
app.js
window._ = require('lodash');
window.Popper = require('popper.js').default;
window.$ = window.jQuery = require('jquery');
require('bootstrap');
login.blade.php
(layout)
<head>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}"></script>
<script>
$(document).ready(function () {
alert("jQuery loaded");
});
</script>
</head>
webpack.mix.js
mix.autoload({
jquery: ['$', 'window.jQuery', "jQuery", "window.$", "jquery", "window.jquery"],
'popper.js/dist/umd/popper.js': ['Popper']
}).js('resources/js/app.js', 'public/js', 'ressources/js/password.js')
.sass('resources/sass/app.scss', 'public/css');
I expect an alert saying jQuery loaded but got an
"Uncaught ReferenceError: $ is not defined"
Answer
In my app.js requirement I changed
window.$ = window.jQuery = require('jquery');
to
global.$ = global.jQuery = require('jquery');
If someone has the reason as to why this does work (and maybe why I shouldn't do that), I'd be happy to hear it.
Related Questions
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → when i send data with ajax(jquery) i don't find the values in controller without form in laravel 5.1
- → DropzoneJS & Laravel - Output form validation errors
- → Knockout JS - How to return empty strings for observable fields
- → How to replace *( in a string
- → Get the calling element with vue.js
- → Sent Variable to Controller via Ajax in Blade
- → AJAX folder path issue
- → Onclick with argument causes javascript syntax error
- → KNockout JS - Automatic reload of ajax call