gpt4 book ai didi

jquery - Laravel mix 未捕获引用错误 : jQuery is not defined

转载 作者:行者123 更新时间:2023-12-03 23:04:17 24 4
gpt4 key购买 nike

我正在使用 laravel mix 来捆绑我的 js 库和代码。我正在尝试使用 ES6 风格的导入,并在可能的情况下使用 ES6 代码。我还需要导入 jQuery 及其库。

所以,我已经导入了 jQuery 和 bootstrap,如下所示:

import "jquery";
import "bootstrap";

首先,当我导入它们时,我得到:

Uncaught ReferenceError: jQuery is not defined
at Object../node_modules/bootstrap/js/transition.js (vendor.js?id=74f7f0c463b407c6bdf5:2449)

这是由于 Bootstrap 未获取 jQuery。

为了解决这个问题,我添加了这个配置来用 jquery 替换 $, jQuery

mix.webpackConfig(webpack => {
return {
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
})
]
};
});

这适用于每个需要 jQuery 的脚本和库。

现在的问题在于我们添加的其他脚本没有使用单个 js 或使用 Blade 部分进行混合。

@section('scripts')
<script type="application/javascript">
window.onload = function() {
if (window.jQuery) {
// jQuery is loaded
console.log("Yeah!");
} else {
// jQuery is not loaded
console.log("Doesn't Work");
}
}
$().ready(function () {
console.log('works');
})
</script>
@endsection

控制台错误显示:

datatables:125 Uncaught ReferenceError: $ is not defined
at datatables:125
(anonymous) @ datatables:125
vendor.js?id=2b5ccc814110031408ca:21536 jQuery.Deferred exception: $(...).uniform is not a function TypeError: $(...).uniform is not a function
at HTMLDocument.<anonymous> (http://localhost/assets/admin/app.js?id=da888f45698c53767fca:18419:18)
at mightThrow (http://localhost/assets/admin/vendor.js?id=2b5ccc814110031408ca:21252:29)
at process (http://localhost/assets/admin/vendor.js?id=2b5ccc814110031408ca:21320:12) undefined
jQuery.Deferred.exceptionHook @ vendor.js?id=2b5ccc814110031408ca:21536
process @ vendor.js?id=2b5ccc814110031408ca:21324
setTimeout (async)
(anonymous) @ vendor.js?id=2b5ccc814110031408ca:21358
fire @ vendor.js?id=2b5ccc814110031408ca:20986
fireWith @ vendor.js?id=2b5ccc814110031408ca:21116
fire @ vendor.js?id=2b5ccc814110031408ca:21124
fire @ vendor.js?id=2b5ccc814110031408ca:20986
fireWith @ vendor.js?id=2b5ccc814110031408ca:21116
ready @ vendor.js?id=2b5ccc814110031408ca:21596
completed @ vendor.js?id=2b5ccc814110031408ca:21606
datatables:122 Doesn't Work
vendor.js?id=2b5ccc814110031408ca:21545 Uncaught TypeError: $(...).uniform is not a function
at HTMLDocument.<anonymous> (app.js?id=da888f45698c53767fca:18419)
at mightThrow (vendor.js?id=2b5ccc814110031408ca:21252)
at process (vendor.js?id=2b5ccc814110031408ca:21320)

当我使用 laravel mix 编译和混合脚本时,问题得到解决,但是当我在 Blade 中编写相同的脚本或不混合使用时,它会显示 jQuery/$ 未定义错误。

在这种情况下最好的方法是什么?

母版页如下所示:

<body>
@yield('body')

<script src="{{ mix('/assets/admin/manifest.js') }}"></script>
<script src="{{ mix('/assets/admin/vendor.js') }}"></script>
<script src="{{ mix('/assets/admin/app.js') }}"></script>

@section('scripts')
@show

@yield('footer')
</body>

最佳答案

根据this answer你需要这样导入jquery

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

关于jquery - Laravel mix 未捕获引用错误 : jQuery is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49351415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com