简体   繁体   English

Laravel 5 混合 app.js 文件创建

[英]Laravel 5 mix app.js file creation

I created a custom JavaScript file with a simple function:我用一个简单的函数创建了一个自定义的 JavaScript 文件:

function picture(soemthing){
    document.getElementById('idXYZ').src = "example.com";
}

Then I added this file to the webpack.mix.js config:然后我将此文件添加到 webpack.mix.js 配置中:

mix.js(['resources/assets/js/app.js', 'resources/assets/js/xyz.js'], 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');

and run: npm run dev.并运行:npm run dev。 npm compiled my script and the picture function was included in the app.js file. npm 编译了我的脚本,图片函数包含在 app.js 文件中。 Now I'd like to use the "picture" function in a blade.php but whenever I call it I get "Uncaught ReferenceError: picture is not defined" .现在我想在blade.php 中使用“图片”函数,但是每当我调用它时,我都会收到“未捕获的引用错误:图片未定义” I checked the page source and found that the picture function is wrapped with a different function我查看了页面源码,发现图片函数被不同的函数包裹了

(function(module, exports) {
    function picture(soemthing) {
        document.getElementById('idXYZ').src = "example.com";
    }
}) 

Should I add some additional namespace before calling the picture function from blade.php or I have something wrong with mix configuration?我应该在从blade.php 调用图片函数之前添加一些额外的命名空间,还是混合配置有问题?

The functions and classes are not exposed to the public, so all JavaScript logic should be written in the JS files.函数和类不公开,所以所有的 JavaScript 逻辑都应该写在 JS 文件中。 If you insist on writing JavaScript logic in the blade file, you could attach the function to the window object.如果您坚持在刀片文件中编写 JavaScript 逻辑,您可以将该函数附加到window对象。

window.picture = function picture(soemthing){
    document.getElementById('idXYZ').src = "example.com";
}

... ...

window.picture()

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

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