📖 Scenario: You are building a simple interactive webpage where users can click a button to increase a counter. This is a common feature on many websites to track clicks or likes.
🎯 Goal: Build a Vue component that shows a button and a number. When the button is clicked, the number increases by one using the v-on directive to handle the click event.
📋 What You'll Learn
Create a Vue component with a data property called count initialized to 0.
Add a button element in the template with a v-on:click directive to call a method.
Create a method called increment that increases count by 1.
Display the current count value in the template.
💡 Why This Matters
🌍 Real World
Buttons that respond to clicks are everywhere on websites, from liking posts to submitting forms. Learning to handle click events is essential for interactive web pages.
💼 Career
Front-end developers use event handling daily to create dynamic user interfaces that respond to user actions smoothly and accessibly.
Progress0 / 4 steps
1
Set up the initial data property
Create a Vue component with a data function that returns an object containing a property called count set to 0.
Vue
Hint
Use ref from Vue to create a reactive variable count initialized to 0.
2
Add the button with v-on:click directive
In the <template>, add a <button> element with a v-on:click directive that calls a method named increment. Inside the button, write the text Click me.
Vue
Hint
Use v-on:click="increment" on the button to listen for clicks.
3
Create the increment method
In the <script setup> section, define a function called increment that increases the value of count by 1 using count.value++.
Vue
Hint
Define a function named increment that adds 1 to count.value.
4
Display the count value
Inside the <div> in the template, below the button, add a <p> element that shows the current value of count using mustache syntax {{ count }}.
Vue
Hint
Use {{ count }} inside a paragraph to show the current count.
Practice
(1/5)
1. What does the v-on directive do in Vue?
easy
A. It fetches data from a server automatically.
B. It listens for user events and runs code when they happen.
C. It styles HTML elements dynamically.
D. It creates new Vue components.
Solution
Step 1: Understand the purpose of v-on
v-on is used to listen to user actions like clicks or key presses.
Step 2: Identify what happens when an event is detected
When the event occurs, v-on runs the specified code or method.
Final Answer:
It listens for user events and runs code when they happen. -> Option B
Quick Check:
Event listening = C [OK]
Hint: Remember: v-on = listen and react to events [OK]
Common Mistakes:
Confusing v-on with styling directives
Thinking v-on fetches data automatically
Assuming v-on creates components
2. Which of the following is the correct syntax to listen for a click event on a button using v-on?
easy
A. Click me
B. Click me
C. Click me
D. Click me
Solution
Step 1: Check the correct v-on syntax
The correct syntax uses a colon after v-on followed by the event name, like v-on:click.
Step 2: Verify the event handler format
The handler should be a method name without parentheses in the directive, so handleClick is correct.
Final Answer:
<button v-on:click="handleClick">Click me</button> -> Option A
Quick Check:
Correct syntax = B [OK]
Hint: Use v-on:event="handler" with colon, no parentheses [OK]
Common Mistakes:
Using v-on-click instead of v-on:click
Adding parentheses in the handler inside directive
Using @click with parentheses (valid but different from v-on syntax)
3. What will happen when the button is clicked in this Vue template?
A. The function declaration is missing parentheses after the name.
B. The event handler should not have parentheses in the template.
C. The alert function is not allowed inside Vue methods.
D. The v-on directive should be written as @click instead.
Solution
Step 1: Check the function declaration syntax
The function submitForm is declared without parentheses, which is invalid syntax in JavaScript.
Step 2: Confirm the correct function syntax
It should be function submitForm() { ... } with parentheses after the name.
Final Answer:
The function declaration is missing parentheses after the name. -> Option A
Quick Check:
Function syntax error = A [OK]
Hint: Always add () after function names in declarations [OK]
Common Mistakes:
Removing parentheses in function declarations
Thinking parentheses are wrong in event handlers
Confusing directive syntax with error cause
5. You want to listen for a key press event on an input using v-on and run a method only when the Enter key is pressed. Which is the correct way to do this in Vue?
hard
A.
B.
C.
D.
Solution
Step 1: Understand Vue event modifiers
Vue supports event modifiers like .enter to listen only for Enter key presses.
Step 2: Check the correct syntax for key modifiers
v-on:keyup.enter="submit()" listens for keyup events only when Enter is pressed and runs submit().
Final Answer:
<input v-on:keyup.enter="submit()" /> -> Option C
Quick Check:
Use .enter modifier for Enter key events = A [OK]
Hint: Use .enter modifier with v-on for Enter key events [OK]
Common Mistakes:
Trying to check event.key manually inside template
Using key attribute instead of event modifier
Forgetting parentheses in method call
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.