📖 Scenario: You are building a simple Vue 3 app where users can click buttons to greet different friends by name.
🎯 Goal: Create a Vue 3 component that shows three buttons labeled with friend names. When a button is clicked, it calls a handler function passing the friend's name as an argument. The handler then updates a message showing the greeting for that friend.
📋 What You'll Learn
Use Vue 3 Composition API with <script setup>
Create a reactive message variable to hold the greeting text
Create a handler function that accepts a name argument and updates the message
Add three buttons with friend names: 'Alice', 'Bob', and 'Charlie'
Pass the friend name as an argument to the click handler using @click
💡 Why This Matters
🌍 Real World
Passing arguments to event handlers is common in interactive web apps, like greeting users, selecting items, or filtering lists.
💼 Career
Understanding how to pass arguments to handlers is essential for building dynamic Vue components and handling user interactions effectively.
Progress0 / 4 steps
1
Set up the friend names array
Create a constant array called friends with these exact strings: 'Alice', 'Bob', and 'Charlie'.
Vue
Hint
Use const friends = ['Alice', 'Bob', 'Charlie'] to create the array.
2
Create a reactive message variable
Import ref from 'vue' and create a reactive variable called message initialized to an empty string ''.
Vue
Hint
Use import { ref } from 'vue' and then const message = ref('').
3
Write the greeting handler function
Create a function called greet that takes one argument name. Inside the function, set message.value to the string `Hello, ${name}!` using a template literal.
Vue
Hint
Define function greet(name) and update message.value inside it.
4
Add buttons and bind click handlers
In the template, use a <button> inside a <template> that loops over friends with v-for. For each friend, create a button showing the friend's name. Add a @click event that calls greet(friend). Also add a <p> below the buttons that displays the reactive message.
Vue
Hint
Use v-for="friend in friends" on buttons and @click="greet(friend)" to pass the name.
Practice
(1/5)
1. In Vue, how do you pass an argument 42 to a click event handler named handleClick in the template?
easy
A. @click="handleClick:42"
B. @click="handleClick"
C. @click="handleClick(42)"
D. @click="handleClick=>42"
Solution
Step 1: Understand Vue event binding syntax
Vue uses @click to bind click events and parentheses to pass arguments.
Step 2: Passing argument 42 to handler
Using @click="handleClick(42)" calls handleClick with argument 42 on click.
Final Answer:
@click="handleClick(42)" -> Option C
Quick Check:
Use parentheses to pass arguments [OK]
Hint: Use parentheses after handler name to pass arguments [OK]
Common Mistakes:
Omitting parentheses when passing arguments
Using colon or arrow instead of parentheses
Passing argument without quotes for strings
2. Which of the following is the correct syntax to pass a string argument 'hello' to a method sayHello in Vue template?
easy
A. @click="sayHello(hello)"
B. @click="sayHello('hello')"
C. @click="sayHello:"hello""
D. @click="sayHello{hello}"
Solution
Step 1: Recognize string argument syntax
Strings must be wrapped in quotes inside parentheses when passed as arguments.
Step 2: Check each option
Only @click="sayHello('hello')" correctly passes the string 'hello'. Others are invalid syntax.
Final Answer:
@click="sayHello('hello')" -> Option B
Quick Check:
Strings need quotes inside parentheses [OK]
Hint: Wrap string arguments in quotes inside parentheses [OK]
A. The handler does not accept the argument passed
B. The formData object is not reactive
C. The event object is missing
D. The button tag is invalid
Solution
Step 1: Check handler parameters
The template passes formData as an argument, but submitForm has no parameters defined.
Step 2: Understand mismatch effect
This causes the argument to be ignored and may confuse the handler logic expecting parameters.
Final Answer:
The handler does not accept the argument passed -> Option A
Quick Check:
Handler must declare parameters to receive arguments [OK]
Hint: Match handler parameters with passed arguments [OK]
Common Mistakes:
Not defining parameters in handler function
Assuming reactive needed for argument passing
Confusing event object with custom arguments
5. You want to pass both a custom argument id and the native event object to a Vue method handleClick. Which is the correct way to do this in the template?
hard
A. @click="handleClick(id, $event)"
B. @click="handleClick($event, id)"
C. @click="handleClick(id)"
D. @click="handleClick()"
Solution
Step 1: Understand argument order
To pass both a custom argument and the event, list them in order inside parentheses.
Step 2: Use $event for native event
$event is Vue's special variable for the native event object.
Step 3: Confirm correct syntax
@click="handleClick(id, $event)" correctly passes id first, then the event.
Final Answer:
@click="handleClick(id, $event)" -> Option A
Quick Check:
Pass custom args before $event [OK]
Hint: Pass $event last to get native event object [OK]
Common Mistakes:
Passing $event before custom arguments
Forgetting to include $event for event object
Calling handler without parentheses
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.