Chapter 9: Vue v-on

v-on directive in Vue 3 (the one that makes your app actually do things when users interact with it). This is the directive for event handling, and in 2026 it’s still the backbone of interactivity in Vue.

v-on (shorthand: @) attaches event listeners to DOM elements or components. Think of it as Vue’s version of addEventListener(‘click’, …) — but declarative, reactive, and much cleaner.

Official description from vuejs.org (as of 2026):

v-on — Attach an event listener to the element. The event type is denoted by the argument. The expression can be a method name, an inline statement, or omitted if there are modifiers present.

Shorthand: @click instead of v-on:click

1. Basic Usage – Hello World Click Example

vue

→ Click any button → count updates reactively → No manual document.querySelector().addEventListener needed — Vue handles it.

2. Passing Arguments & Accessing the Native Event ($event)

vue
  • $event is a special Vue variable that gives you the real browser Event object
  • You can mix arguments: first your params, last $event if needed

3. Event Modifiers – Vue’s Magic Shortcuts

These save you from writing event.preventDefault() or event.stopPropagation() manually.

vue

Most common modifiers (must-know in 2026):

Modifier What it does Common use case
.prevent event.preventDefault() Forms, links
.stop event.stopPropagation() Nested clickable elements
.once Listener removed after first trigger Analytics tracking, one-time actions
.self Only if event.target === element Avoid child clicks bubbling
.passive Improves touch/scroll perf (can’t prevent) Large scrollable areas
.capture Use capture phase instead of bubble Rare, advanced

4. Key Modifiers – Keyboard Events Made Easy

vue

Key aliases: .enter, .tab, .delete, .esc, .space, .up, .down, .left, .right

System modifiers: .ctrl, .shift, .alt, .meta (Windows key / Cmd)

.exact → no extra modifiers allowed

5. Dynamic Event Names (advanced but useful)

vue

6. Object Syntax – Multiple Events at Once (less common)

vue

Quick Reference Table – v-on in 2026

Syntax Example Meaning / Use Case
@click=”increment” Basic click handler
@click=”say(‘Hi’, $event)” With args + event object
@submit.prevent=”save” Form submit without reload
@keyup.enter=”search” Press Enter to trigger
@click.once Run handler only once
@click.stop.prevent Stop bubble + prevent default
@[dynamicEvent]=”handler” Event name from variable
v-on=”{ click: fn1, focus: fn2 }” Multiple events in object form

Pro Tips from Real Projects (Hyderabad 2026 style)

  • Prefer shorthand @ — it’s shorter and everyone uses it
  • For complex logic → always use a method (not inline) → better debugging & reusability
  • Custom components: @my-event=”handler” listens to emitted custom events (we covered in previous “Vue Events” lesson)
  • Performance: .passive on scroll/touch → noticeable on mobile
  • Avoid @click=”count++” in large apps — move to method for testability

Practice: Enhance your todo app — add:

  • @click on delete button
  • @submit.prevent on add form
  • @keyup.enter on input to add todo
  • @dblclick on todo to edit

Any confusion? Want full example with form + keyboard + modifiers? Or how v-on works on custom components vs native elements? Or TypeScript typing for events?

Just tell me — we’ll keep going step by step 🚀

Happy event-handling from Hyderabad! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *