Chapter 8: Vue Events

Vue Events today — this is where your app starts feeling alive and interactive 😄

In Vue (especially Vue 3 in 2026), “events” means two closely related things:

  1. Listening to native DOM events (click, input, keyup, submit, mouseenter, etc.) on HTML elements → handled with v-on directive (shorthand @)
  2. Emitting and listening to custom events between components → child tells parent “something happened!” using $emit / emit → parent reacts with @custom-event

This follows the classic “Data Down, Events Up” pattern:

  • Props send data down from parent → child
  • Events send signals (and data) up from child → parent

We’ll cover both in detail, with modern Composition API + <script setup> examples (what you should use in 2026).

Part 1: Listening to Native DOM Events (v-on / @)

This is how you respond to user actions like clicks, typing, form submits.

Basic syntax:

HTML

Full real example:

vue

Key points:

  • Handler can be:
    • A function name (increment)
    • Inline statement (count++)
    • Multiple with ; separator
  • Automatic $event param → gives you the native DOM event
  • You can pass arguments: @click=”sayHello(‘Rahul’)”

Event Modifiers (super useful shortcuts)

Vue gives modifiers to avoid writing event.preventDefault() or event.stopPropagation() manually.

vue

Popular modifiers (2026 must-know):

  • .prevent → event.preventDefault()
  • .stop → event.stopPropagation()
  • .once → listener removed after first trigger
  • .self → only if event.target === element
  • .passive → improves scroll performance
  • Key: .enter, .tab, .delete, .esc, .space, .up, .ctrl, .shift, .alt, .meta

Part 2: Custom Component Events (Emitting & Listening)

This is how child components communicate up to parents.

Modern way in <script setup> (Vue 3.3+ best practice):

  1. Declare events with defineEmits
  2. Get emit function
  3. Call emit(‘eventName’, payload?)

Child example (InputWithValidation.vue):

vue

Parent usage:

vue

Key modern points:

  • defineEmits([‘update:modelValue’, …]) → declares events (helps IDE, docs, avoids typos)
  • For v-model support → emit ‘update:propName’
  • Payload optional → emit(‘clicked’) or emit(‘clicked’, { id: 123, name: ‘Rahul’ })
  • In older setup() (not <script setup>): const emit = defineEmits(…) still, but return { emit } if needed

Quick Summary Table – Vue Events 2026

Type Directive / Method Example Syntax Use Case
Native DOM @event / v-on:event @click=”increment” Buttons, forms, keyboard, mouse
Native with arg @event @input=”val = $event.target.value” Access event object
Modifier .prevent, .stop, .enter @submit.prevent=”save” Forms, bubbling control, keys
Custom emit (child) defineEmits + emit() emit(‘update’, data) Child → parent communication
Listen custom (parent) @custom-event @update=”handleUpdate” React to child signals
v-model sugar v-model + emit update:xxx v-model=”value” Two-way binding on custom components

Practice this weekend:

  1. Build a todo item component that emits toggle-done and delete events
  2. Parent listens and updates list
  3. Add @keyup.enter on input to add todo

Any part unclear? Want full todo list with events example? Difference between native vs custom? Or how to type events with TypeScript (defineEmits<{ (e: ‘update’, val: string): void }>())?

Just tell me — we’ll debug it together over virtual chai ☕🚀

Keep building interactive UIs from Hyderabad! 💙

You may also like...

Leave a Reply

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