Laravel 5 mix app.js file creation

半城伤御伤魂 提交于 2019-12-13 00:33:04

问题


I created a custom JavaScript file with a simple function:

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

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');

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";
    }
}) 

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


回答1:


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()


来源:https://stackoverflow.com/questions/43504921/laravel-5-mix-app-js-file-creation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!