On the counter at Mrs. Sri's stall there's a bell. The bell does nothing by itself. What makes it useful is Mrs. Sri's rule: "when the bell rings, I come to the cash register".
A button on a page is the same. Without a rule, you can click it many times and nothing happens. Event handling means telling Vue what to run when the user does something: clicks, types, presses a key, or sends a form. Something that happens on the page, such as a click, is called an event. In Vue, the bell is the button, the ring is the click, and Mrs. Sri's rule is written @click="add": when there is a click, run the function add.
@click: run code on a click
Here is an order counter for Iced Tea with two buttons:
const count = ref(0)
function add() {
count.value++
}
---template---
<p>Iced Tea × {{ count }}</p>
<button @click="add">Add</button>
<button @click="count = 0">Cancel</button>
| Event | count |
Screen |
|---|---|---|
| Page appears | 0 | Iced Tea × 0 |
| Click Add | 1 | Iced Tea × 1 |
| Click Add | 2 | Iced Tea × 2 |
| Click Cancel | 0 | Iced Tea × 0 |
v-on:click="add"is the full form, and@click="add"is its shorthand. They mean exactly the same.- The code inside
@clickis called the handler: the code that runs when the event happens. It can be the name of a function from the script (add), or one short statement written right in the template (count = 0). In the template, a ref is used without.value. - In the first exercise, you use a short statement:
<button id="inc" @click="count++">+1</button>.countstarts at 0. One click makes it 1. Two clicks make it 2. - In the second exercise, you use a separate function:
const greet = () => { message.value = "Hello!" }in the script, then@click="greet"in the template. In the script,.valueis required. - Other events are written the same way:
@input,@keyup,@submit,@mouseenter. The name after@is the DOM event's name.
With or without parentheses
This time add has one parameter, name, and puts it into the array orders. Two buttons call add in different ways:
const orders = ref([])
function add(name) {
orders.value.push(name)
}
---template---
<button @click="add('Iced Tea')">Add Iced Tea</button>
<button @click="add">No parentheses</button>
<p>Orders: {{ orders.join(", ") }}</p>
| Written | The name that add receives |
"Orders:" after 1 click |
|---|---|---|
@click="add('Iced Tea')" |
"Iced Tea" |
Iced Tea |
@click="add" |
the click's event object | [object PointerEvent] in Chrome |
@click="add()" |
undefined |
empty |
Without parentheses, Vue gives add the event object: an object with details about that click. That object goes into the array and shows as the text [object PointerEvent], not a dish name. With empty parentheses, name is undefined, which shows as nothing.
- Without parentheses, Vue calls the function itself and gives it the event object. From this object you can find out, for example, which element was clicked. That suits a function with no parameters, like
greetin the second exercise. - With parentheses, you make the call, so you decide its arguments. If the buttons are made with
v-for="m in menu", write@click="add(m.name)", so each button sends its own dish name. - Need an argument and the event object? In the template, the event object is available under the special name
$event. For example@click="add(m.name, $event)", or@input="customer = $event.target.value"to store the text typed into an input incustomer.
Modifiers: .prevent, .stop, .once, and keyboard keys
A modifier is an ending written with a dot after the event name, such as .stop in @click.stop. This code uses three modifiers: .stop, .enter and .prevent.
const log = ref([])
---template---
<div @click="log.push('Iced Tea card opened')">
Iced Tea
<button @click.stop="log.push('add Iced Tea')">Add</button>
</div>
<input @keyup.enter="log.push('search menu')" placeholder="Search menu" />
<form @submit.prevent="log.push('order sent')">
<button>Send order</button>
</form>
<p>{{ log.join(" | ") }}</p>
Each modifier replaces one line of code that you would otherwise write yourself in the handler:
| Modifier | Same as | Example |
|---|---|---|
.prevent |
event.preventDefault() |
@submit.prevent: send a form without reloading the page |
.stop |
event.stopPropagation() |
@click.stop: a click on the button doesn't reach the card around it |
.once |
the handler runs only once | @click.once: clicked three times, it still runs once |
.enter, .esc |
only when that keyboard key is pressed | @keyup.enter="search" |
.preventstops the browser's default action. Without.prevent, when a form is sent, the browser reloads the page, and everything on screen goes back to the start. With@submit.prevent, only your handler runs.@click.preventon a link stops the browser from going to the link's page..stopstops the event from spreading. A click on a button also counts as a click on its parent element (the element around it), here the Iced Tea card's<div>. Without.stop, clicking Add logsadd Iced Teaand thenIced Tea card opened. With.stop, onlyadd Iced Tea.- Modifiers can be chained:
@click.stop.prevent. - In the Playground, pressing Enter in an input inside the form, or clicking a submit button, triggers
@submit, just like in a real project. The difference is that the Playground always stops the page from reloading. In a real project, a form without.preventreloads the page, so still write@submit.prevent.
⚠️ Clicked, but the result is odd
- If the function takes a parameter,
@click="add"without parentheses gives it the event object. What goes into the order is then[object PointerEvent]. Write@click="add(m.name)". <button onclick="count++">is left by Vue as an ordinary HTML attribute. The browser runs it outside the component, wherecountdoesn't exist, so the console writesReferenceError: count is not defined. In the exercise, the checker makes sure your code uses@click.- With
const message = ref("..."), writingmessage = "Hello!"in a script function stops the code withTypeError: Assignment to constant variable.In the script, writemessage.value = "Hello!". - A form without
.preventmakes the page reload, and the text the user typed into the inputs is lost. Write@submit.prevent.
TL;DR: @click="add" (short for v-on:click) runs the function add when the element is clicked. The value can be a function name, or a short statement like count++. Without parentheses, Vue gives the function the event object (details about what happened, such as the clicked element). With parentheses, you decide its arguments, and in the template $event holds that event object. Modifiers (endings with a dot after the event name) replace small bits of code. .prevent calls preventDefault() and stops the browser's default action, such as a form reloading the page. .stop keeps the event from spreading to its parent element (the element around it). .once runs only once, and .enter runs only when Enter is pressed. Don't use a plain HTML onclick, and in the script change a ref through .value.
⚡ @click runs code
Write:
const count = ref(0)
function add() {
count.value++
}
---template---
<p>Iced Tea × {{ count }}</p>
<button @click="add">Add</button>
<button @click="count = 0">Cancel</button>
Meaning: This code counts Iced Tea orders. @click="add" is short for v-on:click="add": when the button is clicked, run the function add. The value can be a function name from the script, or one short statement such as count = 0. In the template, a ref is used without .value.
⚡ With or without parentheses
Write:
<button @click="add('Iced Tea')">Add Iced Tea</button>
<button @click="add">No parentheses</button>
Guess first: function add(name) puts name into the orders. When "No parentheses" is clicked, what goes in?
[object PointerEvent], not a dish name. Without parentheses, Vue calls the function itself and gives it the event object: an object with details about that click.
Meaning: With parentheses, you make the call, so you decide its arguments. If the buttons are made with v-for="m in menu", write @click="add(m.name)", so each button sends its own dish name. If you need the event object too, use $event, the special name for the event object in the template.
⚡ Modifiers: .prevent and .stop
Write:
<form @submit.prevent="log.push('order sent')">
<button>Send order</button>
</form>
Meaning: A modifier is an ending with a dot after the event name. log here is an array of notes. .prevent is the same as event.preventDefault(): it stops the browser's default action, so sending the form doesn't reload the page. .stop is the same as event.stopPropagation(): a click on the button doesn't also count as a click on its parent element (the element around it).