I have a few utility functions inside application.js file that are defined after turbolinks:load for global access across the site…but for the life of me, I can’t get stimulusjs controllers to see them or use them…do I need to specifically import app.js into each stimulus controller? Pls advise. Thanks in advance.
Whenever you define a function somewhere and need to use it else where you need to first
export it and then
import it where you want to use it.
Not really related to Stimulus but more ES6
I’d try one of these two approaches:
- Like adrien mentioned: make a new .js file that contains those utility functions (call it
helpers.js), and use
exportto make them available to be imported. In your controllers, import that helpers.js file into a variable, and those utility functions will be available as sub-properties of that variable
- Not great, but you can assign those functions to the global window scope via
window.doSomething = doSomething;