I recently started exploring Alpine.js and grasped the fundamentals, but I'm facing challenges when trying to move functions outside of inline script tags.
Consider this snippet from index.html
:
<div x-data="{ loading: false }"/>
<button
onClick="post()"
:class="{ 'active': loading === true }"
>
Post Comment
</button>
</div>
If the post()
function were in a file like main.ts
:
const postComment = () => {
this.loading = true;
};
window.postComment = postComment;
How can I prevent this
from being undefined in this scenario?
I've come across numerous examples where functions are contained within index.html
, but none that address situations where they reside in separate files.