Including scripts inside home.js

Hello

Heres the problem – my home.js file is getting way too long, so i’d like to break it down to smaller pieces which could be included in main.js. The problem i’m getting is that whatever home.js function/variable i call in the new file i get the message that function/variable isn’t defined. Of course when i move the function calls back to home.js everything works as expected.

I’m finding similar issues like this and this but still i’m unable to figure out the correct way to do it.

Can you post some of your code? It’s not clear how you’re including your functions and/or loading the JavaScript, or if the error your describe is in the browser, or an ESLint warning/error.

I believe the best was is to export the functions you want to use from the files you’ve split them off into, and then import them where you want to use them. i.e.:

// app/resources/scripts/some_function.js
export default arg => console.log(arg)

// app/resources/scripts/routes/home/js
import some_function from '../some_function.js'

export default {
  init() {
    some_function('hello world')
  },
}
1 Like