简体   繁体   English

JS 文件未加载使用 Webpack、Laravel 混合 Laravel 8

[英]JS files not loading using Webpack, Laravel Mix in Laravel 8

I'm new to Laravel and I'm trying to compile and upload my resources using Laravel Mix.我是 Laravel 的新手,我正在尝试使用 Laravel Mix 编译和上传我的资源。

mix.js('resources/js/app.js','public/js')
    .js('resources/js/users.js','public/js')
    .sass('resources/sass/app.scss', 'public/css')
    .css('resources/css/myStyle.css','public/css/myStyle.css')

These resources compiled successfully;这些资源编译成功; in my users.js file, I have a JS script function that alerts some text on button click.在我的 users.js 文件中,我有一个 JS 脚本 function 会在按钮单击时提醒一些文本。 In my Blade layout, I'm importing files like the following.在我的 Blade 布局中,我正在导入如下文件。

<!-- Scripts -->
<script src="{{ mix('js/users.js') }}"></script>
<!-- <script src="{{mix('/js/NotResourced.js')}}"></script> -->
<script src="{{ mix('js/app.js') }}"></script>

<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<link href="{{ asset('css/myStyle.css') }}" rel="stylesheet">

I understand that Mix compiles the resources and puts them in the public directory so the browser can read them.我知道 Mix 会编译资源并将它们放在公共目录中,以便浏览器可以读取它们。 But when I inspect my loaded files in the browser, the users.js is not among them.但是当我在浏览器中检查我加载的文件时,users.js 不在其中。 I've changed the order in which the files are loaded, but it didn't work.我更改了加载文件的顺序,但它不起作用。 What I noticed is that in my Blade layout, I'm placing them in an HTML head tag.我注意到,在我的 Blade 布局中,我将它们放置在 HTML 头部标签中。 So I changed the position of the script file to before the body close tag position.所以我把脚本文件的position改成了body close tag之前的position。 However, when I inspect my loaded files on the browsers, the app.js file has existed, but the HTML head tag and users.js are not there.但是,当我在浏览器上检查加载的文件时,app.js 文件已经存在,但 HTML 头标签和 users.js 不存在。

Am I doing anything wrong here?我在这里做错什么了吗? And please, what's the proper way to get this done?请问,完成这项工作的正确方法是什么?

I've found what solved my issue, and the answer as follow: Use scripts() method to minify any number of JavaScript files on webpack.mix.js file like this我找到了解决我的问题的方法,答案如下:使用 scripts() 方法来缩小 webpack.mix.js 文件中任意数量的 JavaScript 文件,如下所示

mix.scripts([
'public/js/admin.js',
'public/js/dashboard.js'
 ], 'public/js/all.js');

this is a proper way to compile your JS asset.这是编译 JS 资产的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM