Thuta Learning
Vue.js
BasicWeb DevelopmentBeginner

Event Handling

Vue.jsLesson 820 min

What you'll walk away with

  • Use inline and method event handlers
  • Use the prevent and key modifiers

Explain it simply

Event Handling, in plain language

@click is shorthand for v-on:click. You can write a simple assignment as an inline handler, but for anything more involved, write a dedicated function. Modifiers like .prevent, .stop, and .enter keep your DOM logic clean.

Problem → Why → Solution

The problem — why this concept matters

Forgetting preventDefault on a submit event

A common mistake

Writing overly long logic directly in the template

Core concept — understand how it works

Handle click, submit, and keyboard events with v-on/@ and event modifiers.

Mental model
  1. props or event
  2. component and template
  3. rendered Vue UI

Use inline and method event handlers

Core syntax or pattern

vue
<script setup>
import { ref } from 'vue'
const name = ref('')
function save() {
  console.log(`Saved: ${name.value}`)
}
</script>

<template>
  <form @submit.prevent="save">
    <input v-model="name" @keyup.esc="name = ''">
    <button>Save</button>
  </form>
</template>

Handle click, submit, and keyboard events with v-on/@ and event modifiers.

Read the code step by step

  1. props or event<script setup>

    @click is shorthand for v-on:click. You can write a simple assignment as an inline handler, but for anything more involved, write a dedicated function. Modifiers like .prevent, .stop, and .enter keep your DOM logic clean.

  2. component and template</script>

    Handle click, submit, and keyboard events with v-on/@ and event modifiers.

  3. rendered Vue UI</template>

    Apply Event Handling in a small example and test normal, edge, and failure cases.

Execution flow
  1. props or event
  2. component and template
  3. rendered Vue UI

Success

Use inline and method event handlers

Failure

Forgetting preventDefault on a submit event

Let's break it down simply

@click is shorthand for v-on:click. You can write a simple assignment as an inline handler, but for anything more involved, write a dedicated function. Modifiers like .prevent, .stop, and .enter keep your DOM logic clean.

vue
<script setup>
import { ref } from 'vue'
const name = ref('')
function save() {
  console.log(`Saved: ${name.value}`)
}
</script>

<template>
  <form @submit.prevent="save">
    <input v-model="name" @keyup.esc="name = ''">
    <button>Save</button>
  </form>
</template>
You should see
Saved: Mya

Try it yourself

Build a form that adds a new item when you press Enter and clears the input when you press Escape.

Event Handling — Vue.js

Real-world usage

How to apply it in a real project

Apply Event Handling in a small example and test normal, edge, and failure cases.

Mini challenge

Build it yourself

Build a form that adds a new item when you press Enter and clears the input when you press Escape.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
vue
<script setup>
import { ref } from 'vue'
const name = ref('')
function save() {
  console.log(`Saved: ${name.value}`)
}
</script>

<template>
  <form @submit.prevent="save">
    <input v-model="name" @keyup.esc="name = ''">
    <button>Save</button>
  </form>
</template>

Expected: Use inline and method event handlers

Show hint

Apply Event Handling in a small example and test normal, edge, and failure cases.

Quick quiz 1 / 3

What best describes the core idea of Event Handling?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • props or event → component and template → rendered Vue UI
  • Handle click, submit, and keyboard events with v-on/@ and event modifiers.
  • Forgetting preventDefault on a submit event
  • Apply Event Handling in a small example and test normal, edge, and failure cases.

Event Handling — Vue.js

Easy traps

  • Forgetting preventDefault on a submit event
  • Writing overly long logic directly in the template

Exercise

Build a form that adds a new item when you press Enter and clears the input when you press Escape.

You'll know it worked when: Saved: Mya

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Event Handling | Thuta Learning