Laravel 5 Mix app.js文件创建 [英] Laravel 5 mix app.js file creation

查看:273
本文介绍了Laravel 5 Mix app.js文件创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有简单功能的自定义JavaScript文件:

I created a custom JavaScript file with a simple function:

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

然后我将此文件添加到webpack.mix.js配置:

Then I added this file to the webpack.mix.js config:

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

并运行:npm运行dev. npm编译了我的脚本,并且在app.js文件中包含了picture函数.现在,我想在blade.php中使用图片"功能,但是每当我调用它时,都会得到未捕获的ReferenceError:图片未定义" .我检查了页面源代码,发现picture函数被另一个函数包裹了

and run: npm run dev. npm compiled my script and the picture function was included in the app.js file. 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". 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";
    }
}) 

在从blade.php调用picture函数之前,我应该添加一些其他名称空间吗?还是混合配置有问题?

Should I add some additional namespace before calling the picture function from blade.php or I have something wrong with mix configuration?

推荐答案

函数和类不公开,因此所有JavaScript逻辑都应写入JS文件中. 如果您坚持要在刀片文件中编写JavaScript逻辑,则可以将该函数附加到window对象.

The functions and classes are not exposed to the public, so all JavaScript logic should be written in the JS files. If you insist on writing JavaScript logic in the blade file, you could attach the function to the window object.

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

...

window.picture()

这篇关于Laravel 5 Mix app.js文件创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆