问题
I already have a function in resources/assets/js/common/utils.js
'use strict'
function test(){
console.log('test');
}
How can I define utils.js as a global file and to be use any where? Thanks for read my question!
回答1:
If you need to use it outside of your bundled JavaScript files you can attach it to the window
object
window.test = function () {
console.log('test');
}
Which you can then use in other files, such as your blade template files as
window.test();
Or if you want to use it within your bundled JavaScript files you can import it as a module
// test.js
export default function test() {
console.log('test');
}
// other.js
import test from 'test.js';
test();
As long as you are compiling your files with Laravel Mix as described here in the docs.
来源:https://stackoverflow.com/questions/50484930/how-to-use-laravel-mix-define-global-javascript-function