![](/img/trans.png)
[英]Laravel Mix: Uncaught ReferenceError: ScrollMagic is not defined
[英]Laravel Mix Uncaught ReferenceError: $ is not defined
我搜索了又搜索,但我無法在 SO 上找到問題的答案。 所以這是我的問題。 我正在嘗試使用 Laravel Mix 在全球范圍內加載 jQuery。 我已經嘗試修改各種文件,但似乎沒有任何效果……我仍然收到“$ is not defined”錯誤。
這是我的代碼。
Bootstrap.js
window._ = require('lodash');
window.Popper = require('popper.js').default;
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap');
} 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
mix.js('resources/assets/js/app.js', 'public/js')
.js('resources/assets/js/select2.min.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css')
.sass('resources/assets/sass/admin/app.scss', 'public/css/admin')
.copy('resources/assets/css/fontawesome.css', 'public/css')
.copy('resources/assets/css/select2.min.css', 'public/css')
.copy('resources/assets/webfonts', 'public/webfonts')
.copy('resources/assets/js/tinymce', 'public/js/tinymce');
mix.browserSync('http://localhost:8000');
我得到的錯誤:
未捕獲的 ReferenceError:$ 未定義
@section('scripts') 中 create.blade.php 內部的代碼
<script>
$(function(){
alert();
});
</script>
{{-- Tiny MCE --}}
<script src="/js/tinymce/tinymce.min.js"></script>
<script>
tinymce.init({
selector:'textarea',
plugins: 'link',
menubar: false,
branding: false,
resize: false,
statusbar: false,
force_br_newlines : false,
force_p_newlines : false,
forced_root_block : '',
toolbar: ['undo redo | cut copy paste | removeformat',
'bold italic underline | link | outdent indent | alignleft aligncenter alignright alignjustify alignnone',],
});
</script>
{{-- Image Javascript --}}
<script type="text/javascript">
$(function() {
// We can attach the `fileselect` event to all file inputs on the page
$(document).on('change', ':file', function() {
var input = $(this),
numFiles = input.get(0).files ? input.get(0).files.length : 1,
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [numFiles, label]);
});
// We can watch for our custom `fileselect` event like this
$(document).ready( function() {
$(':file').on('fileselect', function(event, numFiles, label) {
var input = $(this).parents('.input-group').find(':text'),
log = numFiles > 1 ? numFiles + ' files selected' : label;
if( input.length ) {
input.val(log);
} else {
if( log ) alert(log);
}
});
});
});
</script>
最后是我的布局文件
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>
{{-- Page Specific Scripts --}}
@yield('scripts')
</body>
我究竟做錯了什么???!?
Console.log(e) 什么都不返回...這意味着 jquery 應該正確加載但不是。
如果您在 laravel mix 中使用jquery
,請在app.js
這種方式導入:
window.$ = window.jQuery = require('jquery');
我在 Laravel 6 上遇到了同樣的問題。你可以做兩件事......
或者從 script 標簽中刪除 de defer
;
<script src=”{{ asset(‘js/app.js’) }}” defer></script>
應該:
<script src=”{{ asset(‘js/app.js’) }}"></script>
或者像這樣在你的 Blade 模板中使用 jQuery 加載事件之后;
<script>
window.addEventListener('load', function() {
console.log($);
});
</script>
當您希望在完全解析頁面后執行腳本時,將使用腳本標記中的defer
屬性。 因此,您只能在頁面加載並且腳本完全加載后才能使用加載的腳本。
對我有用的,
npm install --save jquery
global.$ = global.jQuery = require('jquery');
npm install && npm run dev
希望能幫助到你!
我在 laravel 中使用 jquery 也有同樣的錯誤。 我已經通過 npm npm install jquery
安裝 jquery 解決了這個問題,然后我在我需要的腳本中導入了 jquery,如下所示:
import $ from "jquery";
那么$
將在 js 文件中可用。 之后,我在 entry-point.js 中調用了我的 js 文件並運行npm run dev
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.