![](/img/trans.png)
[英]Laravel Mix 'watch' not compiling when webpack.mix.js updated
[英]How to add global functions in Laravel webpack.mix.js?
我正在使用Laravel mix来编译我的资产,并将它们组合成单个文件。 我创建了一个新的js文件,并在其中编写了一些方法。 我将此文件与app.js文件合并。 当我尝试调用这些方法时,从刀片文件中得到function not defined
的错误function not defined
。 我无法访问这些方法。
请帮助我调用这些方法。
Laravel Mix代码
mix.js([ 'js/app.js',
'js/custom.js'
], 'public/js'),
.....
Custom.js
function test(){
alert('test')
}
test.blade.php
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript">
test();
</script>
错误:未定义测试
只需将其分配给window即可 。 所以在您的Custom.js中
window.test = function(){
alert('test')
}
并在您的test.blade.php中使用它:
<script type="text/javascript">
window.test();
</script>
尝试这个:
custom.js
function test(){
alert('test')
}
export { test };
app.js
...
import { test } from './custom.js';
不仅仅是将新文件连接到app.js
的末尾,更干净的方法(imho)是使webpack.mix.js
保持不变。
在resources/assets/js
创建custom.js
文件,只需添加require('./custom');
在resources/assets/js/app.js
的底部
然后,在其余的构建过程中将需要此功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.