I'm just starting out with Alpine.js, I understand the basics but I'm having trouble applying them when moving functions outside of inline script tags.
For example, in index.html
:
<div x-data="{ loading: false }"/>
<button
onClick="post()"
:class="{ 'active': loading === true }"
>
Post Comment
</button>
</div>
if post()
was in main.ts
:
const postComment = () => {
this.loading = true;
};
window.postComment = postComment;
How do I make it so that this
isn't undefined?
I've found plenty of examples where functions are kept within index.html
, but none where they're in a separate file.