0 day streak
javascript / beginner
Snippet

Handling Click Events

To listen to DOM events, we use the v-on directive, which is commonly shortened to the @ symbol. It calls a function defined in your script when the event occurs.

snippet.js
javascript
1
2
3
4
5
6
7
8
9
<template>
<button @click="showAlert">Click Me!</button>
</template>
<script setup>
const showAlert = () => {
alert('Button was clicked!');
};
</script>
vue
Breakdown
1
@click="showAlert"
Listens for the click event and triggers the showAlert function.
2
const showAlert = () => { ... }
Defines the logic that should run when the button is pressed.