How to Add a JavaScript Module
April 07, 2021 | 10 min read
6. Build a template file
Now, we’ll create the template that will enable JavaScript.
In the templates directory, add the file
app/code/Oscprofessionals/Js/view/frontend/templates/hello.phtml:
This code is used to enable our module. It might look a little strange to those who used the requirejs before. As mentioned earlier, Magento 2 has its own unique JavaScript code execution process.
One way is to use the data-mage-initattribute that uses the JSON object as a parameter (as can be seen in this example). Each key of the object corresponds to the module, and the value is the config. In this case, the JavaScript module should return a function with two parameters: config and element. (This can be seen again in our example)