Adzz
How do i reference javascript in app.js in a Phoenix template?
How do I refer to javascript defined app.js in a template. Say I want to add an onsubmit to a form… I’ve tried this at the bottom of a template:
<script type="text/javascript">
require("static/js/app.js")
</script>
But I get Uncaught ReferenceError: require is not defined However I know the js in app.js is there on the page because if I put this in app.js:
console.log("loaded")
I see that in the console. But if I do this in app.js:
export const log = (x) => alert(x)
Then put this in the bottom of my template:
<script type="text/javascript">
log("From template")
</script>
I get log is not defined
Most Liked
harmon25
Depending on the browser might be able to use import
require is a nodejs keyword.
harmon25
If you were not using webpack, import/export would just work.
To do what you want with webpack requires some webpack foo.
If you don’t need asset bundling, could probably skip webpack. But good luck using any node dependencies…
Hope LiveView serves you well!
EDIT: webpack 5s support for module federation would help here I imagine…https://webpack.js.org/concepts/module-federation/







