Vue Event Modifiers
Event handlers very often need to call event.preventDefault(), event.stopPropagation() or check which key was pressed. Vue lets you express these common tasks declaratively with modifiers: dot-suffixes on v-on, such as @submit.prevent or @keyup.enter. Your methods stay focused on the actual logic instead of DOM details.
Event modifiers
| Modifier | Effect |
|---|---|
.prevent |
Calls event.preventDefault() (e.g. stop a form from reloading the page) |
.stop |
Calls event.stopPropagation() — the event does not bubble to parents |
.self |
Only runs if the event happened on the element itself, not a child |
.once |
The handler runs at most once |
.capture |
Listens in the capture phase (parents first) |
.passive |
Tells the browser the handler won't call preventDefault() (smooth scrolling) |
.prevent on forms
Submitting a form normally reloads the page. With @submit.prevent, Vue prevents that, so your method can handle the data.
<div id="app">
<form @submit.prevent="subscribe">
<input v-model="email" type="email" placeholder="you@example.com" required>
<button>Subscribe</button>
</form>
<p v-if="list.length">Subscribed: {{ list.join(', ') }}</p>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
data: () => ({ email: '', list: [] }),
methods: {
subscribe() {
this.list.push(this.email);
console.log('Subscribed', this.email);
this.email = '';
}
}
}).mount('#app');
</script>.stop and .self
Events bubble up from the clicked element through all its ancestors. A click on a button inside a card also counts as a click on the card. .stop cuts the bubbling off; .self ignores clicks that started on a child.
<style>
.card { padding: 16px; background: #f0f9ff; border: 1px solid #93c5fd; margin-bottom: 8px; cursor: pointer; }
.overlay { padding: 24px; background: #0003; }
.dialog { background: white; padding: 12px; }
</style>
<div id="app">
<div class="card" @click="log('card clicked')">
Product card
<button @click="log('button clicked')">Buy (bubbles)</button>
<button @click.stop="log('button clicked, no bubbling')">Buy (.stop)</button>
</div>
<div class="overlay" @click.self="log('overlay clicked — close dialog')">
<div class="dialog">Clicking here does not close the dialog. Click the grey area.</div>
</div>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
methods: {
log(msg) { console.log(msg); }
}
}).mount('#app');
</script>Click the first "Buy" button: you see two log lines, because the card also receives the click. The .stop button logs only once.
.once
<div id="app">
<button @click.once="claim">Claim your free coupon</button>
<p>{{ status }}</p>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
data: () => ({ status: 'Not claimed yet', claims: 0 }),
methods: {
claim() {
this.claims++;
this.status = `Coupon claimed (${this.claims} time). Clicking again does nothing.`;
}
}
}).mount('#app');
</script>Chaining modifiers
Modifiers can be chained. The order matters for some combinations:
<a @click.stop.prevent="open">…</a> <!-- both stop and prevent -->
<div @click.self.prevent="close">…</div> <!-- prevent only clicks on the div itself -->Key modifiers
For keyboard events you can name the key that should trigger the handler.
| Modifier | Key |
|---|---|
.enter |
Enter |
.tab |
Tab |
.delete |
Delete and Backspace |
.esc |
Escape |
.space |
Space |
.up .down .left .right |
Arrow keys |
.page-down, .a, .f1… |
Any KeyboardEvent.key value in kebab-case |
System modifier keys
.ctrl, .alt, .shift and .meta (⌘ on Mac, ⊞ on Windows) require that key to be held down. Add .exact to require that no other system keys are pressed.
<div id="app">
<p>Focus the input and try the shortcuts:</p>
<input
placeholder="Shortcuts work here"
@keydown.enter.exact="log('Enter: send')"
@keydown.shift.enter="log('Shift+Enter: new line')"
@keydown.ctrl.s.prevent="log('Ctrl+S: save (browser dialog prevented)')"
@keydown.esc="log('Escape: cancel')">
<ul><li v-for="(line, i) in lines" :key="i">{{ line }}</li></ul>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
data: () => ({ lines: [] }),
methods: {
log(msg) { this.lines.unshift(msg); }
}
}).mount('#app');
</script>Mouse button modifiers
.left, .right and .middle restrict mouse events to one button. @click.right.prevent is a quick way to build a custom context menu.
Tip: Use
.passiveonscroll,wheelandtouchmovelisteners that don't need to block scrolling. Never combine.passivewith.prevent— the browser would ignore the prevent and warn.
Note: On components,
v-onlistens to component events (emitted with$emit), not DOM events. Modifiers like.oncestill work there;.preventand.stoponly make sense for native DOM events.
Test yourself
Which listener stops a form from reloading the page on submit?
@submit.stop@submit.prevent@submit.once@submit.self
A click on a child button should NOT trigger the parent's @click. Which modifier goes on the child's listener?
.self.capture.stop.exact
What does @keydown.ctrl.enter.exact="send" require?
- Ctrl + Enter with no other system keys held
- Ctrl or Enter
- Enter, then Ctrl
- Ctrl + Enter, Shift allowed