Skip to content Skip to sidebar Skip to footer

Is It Possible To Import Js Function Into A Js.erb File?

Is it possible to import a JS function to be used in a js.erb file? For example: // hello_world.js const helloWorld = () => console.log('hello world'); export default helloWorld

Solution 1:

By making the function available on the window you can call it inside your view or js.erb file.

// hello_world.jsconsthelloWorld = () => console.log('hello world');

window.helloWorld = helloWorld;
// our_file.js.erbhelloWorld();

I'm not sure if this is best practice but it's a workaround.

Post a Comment for "Is It Possible To Import Js Function Into A Js.erb File?"