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.
- props or event
- component and template
- rendered Vue UI
Use inline and method event handlers
Core syntax or pattern
<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
- 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.
- component and template
</script>Handle click, submit, and keyboard events with v-on/@ and event modifiers.
- rendered Vue UI
</template>Apply Event Handling in a small example and test normal, edge, and failure cases.
- props or event
- component and template
- 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.
<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>Saved: MyaTry 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
<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
Quick quiz 2 / 3
Quick quiz 3 / 3
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