Event Arguments
An event argument is a value passed to an event handler. It lets one handler serve many buttons with different values.
Passing a value
Each button passes its own flavor.
<button @click="pick('Mint')">Mint</button>Passing the event
The button passes the event itself.
<button @click="show($event)">Show</button>Reading the event
The handler reads the event type.
function show(event) {
console.log(event.type)
}Example
<script setup>
import { ref } from 'vue'
const picked = ref('None')
function pick(flavor) {
picked.value = flavor
}
</script>
<template>
<div>
<button @click="pick('Mint')">Mint</button>
<button @click="pick('Berry')">Berry</button>
<p>Picked: {{ picked }}</p>
</div>
</template>
Key points
- $event holds the event object, with details about the event.
- Passing $event hands the event itself to the handler.
- A handler can also take a plain value, like a word or a number.
- A plain value is written in the template call, like pick('Mint').
- A value and $event can be passed together, like pick('Mint', $event).
In this example, picked starts as the string None. pick stores a flavor in picked. Each button passes its own flavor to pick. Clicking Berry stores Berry in picked. The paragraph shows the current pick.
Note
$event only has meaning inside an event handler.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
How can one handler serve many buttons?