How to use laravel mix define global javascript function

丶灬走出姿态 提交于 2019-12-07 23:04:03

问题


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

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