3

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 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.