Chapter 80: Vue v-on Directive

V-on (99% of the time written with its beautiful shorthand: @)

This directive is Vue’s official way to listen to DOM events (click, submit, keyup, mouseenter, change…) and custom events emitted from child components.

In plain words:

“Hey Vue, whenever this element (or child component) fires this event, please run my function.”

Without v-on / @, you would have to manually do addEventListener in onMounted and removeEventListener in onBeforeUnmount — ugly, error-prone, and not reactive.

With @click, @submit, @keyup.enter… Vue handles everything for you: attaching, detaching, passing the event object, supporting modifiers, and more.

1. Basic Syntax – The Shorthand Everyone Uses

HTML

Shorthand rule (burn this into your brain)

@ = v-on:

So everywhere you see @click, @submit, @keydown.enter — it’s just sugar for v-on:click, v-on:submit, v-on:keydown.enter

2. Core Features & Modifiers (What Makes @ So Powerful)

Vue gives you event modifiers that save you from writing a lot of boilerplate JavaScript.

Modifier What it does Typical usage example Equivalent vanilla JS
.stop event.stopPropagation() @click.stop — stop bubbling to parent e.stopPropagation()
.prevent event.preventDefault() @submit.prevent — prevent form submit reload e.preventDefault()
.capture Add listener in capture phase @click.capture { capture: true }
.self Only trigger if event.target === element @click.self — ignore child clicks if (e.target !== el)
.once Listener removed after first trigger @click.once — track one-time analytics el.addEventListener(…, { once: true })
.passive Improves scroll/touch performance @touchmove.passive — large scroll areas { passive: true }

Key combinations (very common)

HTML

3. Real, Complete Example – Form with Multiple Event Types

vue

Quick Summary Table – v-on / @ Modifiers & Common Patterns

Modifier / Syntax What it does Typical real-world usage
@click Listen to click event Buttons, cards
@submit.prevent Prevent form reload Almost every <form>
@keyup.enter Only on Enter key Submit form on input Enter
@click.stop Stop event bubbling Click inside modal shouldn’t close it
@click.once Run only once Analytics tracking, onboarding tooltips
@click.self Only if clicked directly on element (not children) Container clicks ignoring child buttons
@click.right.prevent Right-click + prevent context menu Custom context menus
@keydown.ctrl.enter Ctrl + Enter Submit large textareas
@click.left Only left-click (not middle/right) Distinguish mouse buttons

Pro Tips from Real Projects (Hyderabad 2026)

  • Always use .prevent on @submit — prevents page reload
  • Use .stop when you have nested clickable elements (modal overlay vs modal content)
  • Use .once for one-time actions (track first click, onboarding)
  • Use .self to ignore child clicks (click on modal backdrop to close)
  • Use .passive on scroll/touch events for better performance
  • Combine modifiers: @click.stop.prevent.self — very common in modals
  • For custom events from child components → @custom-event=”handler”

Your mini practice task:

  1. Build the form above
  2. Add validation messages with v-if
  3. Use @keyup.enter.prevent on inputs to submit
  4. Add @click.stop on modal content (once you add modal)
  5. Add @click.once on a button to track first click

Any part confusing? Want full examples for:

  • Modal with @click.self on overlay + @click.stop on content?
  • Form with @submit.prevent + @keydown.ctrl.enter?
  • Custom component emitting events + parent listening with @?
  • All common modifiers in one realistic dashboard card?

Just tell me — we’ll build the next event-rich component together 🚀

You may also like...

Leave a Reply

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