Vue Events
Users interact with a page through events: clicks, key presses, typing, mouse moves, form submissions. In Vue you listen to DOM events with the v-on directive, usually written with its shorthand @.
<button v-on:click="count++">Add</button> <!-- long form -->
<button @click="count++">Add</button> <!-- shorthand -->Inline handlers
The simplest handler is a short JavaScript expression written directly in the template. It can read and change your data.
<div id="app">
<p>Likes: {{ likes }}</p>
<button @click="likes++">👍 Like</button>
<button @click="likes = 0">Reset</button>
<button @dblclick="likes += 10">Double-click for +10</button>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({ data: () => ({ likes: 0 }) }).mount('#app');
</script>Method handlers
When the logic is longer than one line, put it in a method and give @click the method name. Vue calls it with the native DOM event object as the first argument.
<style>
.pad { width: 260px; height: 120px; background: #ecfdf5; border: 2px dashed #42b883;
display: grid; place-items: center; font-family: sans-serif; user-select: none; }
</style>
<div id="app">
<div class="pad" @mousemove="track" @mouseleave="pos = 'outside'">
{{ pos }}
</div>
<button @click="greet">Greet</button>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
data: () => ({ pos: 'Move the mouse here' }),
methods: {
track(event) {
this.pos = `x: ${event.offsetX}, y: ${event.offsetY}`;
},
greet(event) {
console.log('Clicked a', event.target.tagName, 'element');
}
}
}).mount('#app');
</script>Passing arguments
To pass your own arguments, call the method in the template. If you also need the event, pass the special variable $event, or use an arrow function.
<div id="app">
<p>Wallet: {{ money }} coins</p>
<button @click="spend(5)">Buy candy (5)</button>
<button @click="spend(20)">Buy a book (20)</button>
<button @click="spend(50, $event)">Buy shoes (50)</button>
<button @click="(e) => spend(1, e)">Tip (1)</button>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
data: () => ({ money: 60 }),
methods: {
spend(amount, event) {
if (amount > this.money) {
console.log(`Not enough coins for ${amount}!`);
return;
}
this.money -= amount;
if (event) console.log('Button text:', event.target.textContent);
}
}
}).mount('#app');
</script>| You write | Vue calls |
|---|---|
@click="save" |
save(event) |
@click="save()" |
save() — no event |
@click="save(id)" |
save(id) |
@click="save(id, $event)" |
save(id, event) |
@click="(e) => save(id, e)" |
save(id, e) |
Common DOM events
| Event | Fires when… |
|---|---|
click, dblclick |
the element is clicked / double-clicked |
input |
the value of an input changes (every keystroke) |
change |
an input is committed (blur, select chosen, checkbox toggled) |
keydown, keyup |
a key is pressed / released |
submit |
a form is submitted |
focus, blur |
an element gains / loses focus |
mouseenter, mouseleave, mousemove |
the mouse enters, leaves or moves over an element |
Listening to keyboard and input events
<div id="app">
<input @input="onInput" @keyup.enter="submit" placeholder="Type, then press Enter">
<p>Characters: {{ length }}</p>
<p v-if="sent">Sent: "{{ sent }}"</p>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
data: () => ({ length: 0, sent: '' }),
methods: {
onInput(e) { this.length = e.target.value.length; },
submit(e) {
this.sent = e.target.value;
e.target.value = '';
this.length = 0;
}
}
}).mount('#app');
</script>The .enter part of @keyup.enter is a key modifier: the handler only runs for the Enter key. Modifiers are the topic of the next lesson.
Multiple handlers
You can run several statements in one inline handler by separating them with a semicolon, or listen to the same event with a method that calls others:
<button @click="count++; log('clicked')">Click</button>Tip: Keep templates readable. If an inline handler grows longer than a short expression, move it into a method with a descriptive name like
addToCartortoggleMenu.
Note: Vue removes the listeners automatically when the element or component is destroyed, so you never have to call
removeEventListenerforv-onlisteners.
Test yourself
What is the shorthand for v-on:click?
:click#click@click*click
With @click="remove(item, $event)", what is $event?
- The native DOM event object
- The removed item
- The component instance
- A Vue-specific event bus
What does Vue pass to @click="save" (method name without parentheses)?
- Nothing
- The component data
- The DOM event
- The element's id