📖 Scenario: You are building a simple interactive webpage where users can click a button to increase a counter. This helps understand how Vue listens and reacts to user actions.
🎯 Goal: Create a Vue component that shows a number and a button. When the button is clicked, the number increases by one. This demonstrates why event handling is important to make web pages interactive.
📋 What You'll Learn
Use Vue 3.4+ with Composition API and <script setup>
Create a reactive counter variable
Add a button with a click event handler
Update the counter when the button is clicked
Display the current counter value
💡 Why This Matters
🌍 Real World
Event handling is how websites respond to user actions like clicks, taps, or key presses. This is essential for interactive features like buttons, forms, and games.
💼 Career
Understanding event handling is a core skill for frontend developers working with Vue or any modern web framework to build responsive user interfaces.
Progress0 / 4 steps
1
Set up the counter variable
In a Vue component with <script setup>, import ref from 'vue' and create a reactive variable called counter initialized to 0.
Vue
Hint
Use ref(0) to create a reactive number variable.
2
Add a button to trigger events
Inside the <template>, add a <button> element with the text Click me. This button will later handle clicks.
Vue
Hint
Use a simple <button> tag with the text inside.
3
Add event handling to update counter
Add a @click event handler to the <button> that increases the counter.value by 1 when clicked.
Vue
Hint
Use @click="counter.value++" on the button to handle clicks.
4
Display the current counter value
Inside the <template>, add a <p> element that shows the text Clicked: followed by the current counter.value.
Vue
Hint
Use mustache syntax {{ counter.value }} to show the reactive value.
Practice
(1/5)
1. Why is event handling important in a Vue app?
easy
A. It lets the app respond to user actions like clicks and typing.
B. It automatically styles the app based on user preferences.
C. It stores data permanently in the browser.
D. It creates new HTML elements without user input.
Solution
Step 1: Understand event handling purpose
Event handling allows the app to react when users do things like click buttons or type text.
Step 2: Compare options to event handling role
Only It lets the app respond to user actions like clicks and typing. describes reacting to user actions, which is the core of event handling.
Final Answer:
It lets the app respond to user actions like clicks and typing. -> Option A
Quick Check:
Event handling = respond to user actions [OK]
Hint: Event handling = app reacts to user actions [OK]
Common Mistakes:
Confusing event handling with styling or data storage
Thinking event handling creates HTML elements automatically
2. Which is the shorthand way to listen for a click event on a button in Vue?
easy
A. Click me
B. Click me
C. Click me
D. Click me
Solution
Step 1: Recall Vue event binding syntax
Vue uses @event shorthand or v-on:event to listen for events.
Step 2: Identify correct shorthand usage
Click me uses @click, the recommended shorthand for click events in Vue templates.
Final Answer:
<button @click="handleClick">Click me</button> -> Option B
Quick Check:
Vue event shorthand = @event [OK]
Hint: Use @event for Vue event listeners [OK]
Common Mistakes:
Using plain HTML event attributes like onClick
Forgetting the @ or v-on: prefix
Using invalid attribute names like click
3. What will happen when the button is clicked in this Vue component?
C. The method name in the template and script do not match in case.
D. The button tag is not closed properly.
Solution
Step 1: Compare method name in template and script
The template calls submitForm but the method is named submitform (lowercase f), causing a mismatch.
Step 2: Understand Vue method matching
Vue is case-sensitive, so the method won't be found and the click won't work.
Final Answer:
The method name in the template and script do not match in case. -> Option C
Quick Check:
Method names must match exactly [OK]
Hint: Check method names match exactly (case sensitive) [OK]
Common Mistakes:
Ignoring case sensitivity in method names
Thinking parentheses are required in @click
Assuming console.log syntax is wrong
5. You want to update a message on the page when a user types in an input box. Which Vue setup correctly handles the input event to update the message reactively?
<template>
<input @input="updateMessage" />
<p>Message: {{ message }}</p>
</template>
<script setup>
import { ref } from 'vue'
const message = ref('')
function updateMessage(event) {
// What should go here?
}
</script>
hard
A. message.value = event.target.value
B. message = event.target.value
C. this.message = event.target.value
D. message.value = event.value
Solution
Step 1: Understand Vue ref and event object
message is a ref, so to update its value, use message.value. The event object has the input value in event.target.value.
Step 2: Choose correct assignment
Assigning message.value = event.target.value updates the reactive ref correctly.
Final Answer:
message.value = event.target.value -> Option A
Quick Check:
Update ref with message.value = event.target.value [OK]
Hint: Use message.value = event.target.value to update ref [OK]
Common Mistakes:
Assigning directly to message without .value
Using this.message in script setup (no this context)
Using event.value instead of event.target.value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.