🖱️ Vue v-on – Handling Events in Vue.js (2025 Guide)
🧲 Introduction – What is v-on
in Vue?
In Vue.js, user interaction is key to dynamic web applications. The v-on
directive lets you listen to DOM events and execute methods in response—everything from clicks and hovers to key presses and form inputs.
🎯 In this guide, you’ll learn:
- How to use
v-on
to handle events - The shorthand syntax
@
- How to pass arguments and use the event object
- Best practices and modifiers for event control
🔧 What is v-on
?
The v-on
directive binds native DOM events (like click
, input
, submit
) to Vue instance methods.
✅ Basic Syntax:
<button v-on:click="sayHello">Click Me</button>
🔁 Shorthand:
<button @click="sayHello">Click Me</button>
🧠 Explanation:
@click
listens for the button click- Executes
sayHello
method defined inmethods
🧪 Example – Basic Event Handling
<template>
<div>
<button @click="increment">+1</button>
<p>Count: {{ count }}</p>
</div>
</template>
<script>
export default {
data() {
return { count: 0 };
},
methods: {
increment() {
this.count++;
}
}
}
</script>
🧠 Result: Clicking the button increases count
by 1 and updates the UI reactively.
📥 Passing Arguments to Event Handlers
<button @click="greet('Vue')">Greet</button>
methods: {
greet(name) {
alert(`Hello, ${name}!`);
}
}
🧠 Note:
To access the event object with arguments, use $event
:
<button @click="handleClick('btn', $event)">Click</button>
methods: {
handleClick(label, event) {
console.log(label, event.target);
}
}
🧩 Inline JavaScript in v-on
You can write simple inline expressions:
<button @click="count++">Increment</button>
⚠️ Avoid complex logic inline. Use methods for better readability.
🔒 Event Modifiers
Vue provides modifiers to handle common event use-cases:
Modifier | Effect |
---|---|
.stop | event.stopPropagation() |
.prevent | event.preventDefault() |
.capture | Use event capturing mode |
.self | Only trigger if event.target is element |
.once | Run the handler only once |
.passive | For scroll & touch performance |
🧪 Example:
<form @submit.prevent="onSubmit">
<input type="text" />
</form>
🧠 Prevents page reload when form is submitted.
🎹 Listening to Keyboard Events
<input @keyup.enter="submitInput" />
Vue supports:
.enter
,.tab
,.esc
,.space
,.delete
.up
,.down
,.left
,.right
🧰 Event Chaining with Modifiers
You can combine modifiers:
<button @click.stop.prevent="doSomething">Do</button>
🧠 Prevents both bubbling and default behavior in one line.
🧬 Real-World Example – Interactive To-Do App
<template>
<div>
<input v-model="task" @keyup.enter="addTask" />
<ul>
<li v-for="(t, i) in tasks" :key="i">{{ t }}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return { task: '', tasks: [] };
},
methods: {
addTask() {
if (this.task) {
this.tasks.push(this.task);
this.task = '';
}
}
}
}
</script>
🧠 Result: Pressing Enter adds a task to the list using @keyup.enter
.
📌 Summary – Recap & Next Steps
The v-on
directive is a cornerstone of interactivity in Vue. From button clicks to keyboard shortcuts and form handling, v-on
connects your templates with your business logic clearly and efficiently.
🔍 Key Takeaways:
- Use
v-on
or@
to bind event listeners - Define clean, reusable methods in your Vue instance
- Leverage modifiers for concise control
- Pass arguments and
$event
as needed
⚙️ Real-World Relevance:
From simple counters to complex UIs, event handling powers user interaction. Mastering v-on
gives you full control over frontend behavior.
❓ FAQ Section
❓ What is v-on
in Vue?
✅ It binds a native event (like click
, submit
, keyup
) to a method in your Vue component.
❓ How do I pass parameters in v-on
?
✅ Use parentheses:
<button @click="greet('World')">Say Hi</button>
❓ What is the @
symbol in Vue?
✅ It’s shorthand for v-on
. For example, @click="doIt"
is the same as v-on:click="doIt"
.
❓ What are event modifiers in Vue?
✅ They simplify handling of preventDefault()
, stopPropagation()
, and other behaviors:
<button @click.prevent="save">Save</button>
❓ Can I use v-on
for custom component events?
✅ Yes. Use v-on
or @
to listen for $emit
events:
<child-component @custom-event="handleEvent"></child-component>
Share Now :