To use Alpine.js in JavaScript, first include the alpine.js script in your HTML file:
83 chars2 lines
Then, you can start using Alpine.js in your JavaScript like this:
index.tsx367 chars15 lines
In this example, we define a new Alpine.js component called app
that has a message
property and a toggle
method. Then, we mount the component on a specific element in our HTML by calling Alpine.mount('#app', app())
.
Now, if we have an element in our HTML with an id
of app
and a reference to x-data="app()"
, we can use the component like this:
122 chars5 lines
This will render a heading that displays the current value of the message
property, and a button that toggles the value when clicked.
Alpine.js is a powerful tool for creating declarative UIs in JavaScript, and it can dramatically simplify your front-end development workflow.
gistlibby LogSnag