Closed alancwoo closed 3 years ago
In this case you can either expose your method to the global scope explicitly
window.hello = () => alert('Hello!')
Which kind of defeats the whole purpose of using javascript modules, which is not to pollute the global scope. So I would rather register the click event in your javascript file.
<!-- templates/home.php -->
<span class="hello-button">hello<span>
// src/templates/home/index.js
const hello = () => alert('Hello!')
document.querySelector('.hello-button').addEventListener('click', hello)
Ok this makes sense. I'm using AlpineJS and was hoping to define functions I could use with @click
etc, so have found other ways around it.
Thank you :)
Hello again, sorry for another question - say I have something like
src/templates/home/index.js
:and want to call the method from the php script at
templates/home.php
:What is the right way to reference this method? (it is undefined in the general scope).