0
0
Vueframework~20 mins

Passing arguments to handlers in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Event Handler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be the output when clicking the button?

Consider this Vue 3 component using the Composition API. What will be logged to the console when the button is clicked?

Vue
<template>
  <button @click="handleClick(5)">Click me</button>
</template>

<script setup>
import { ref } from 'vue'

function handleClick(num) {
  console.log(num * 2)
}
</script>
A10
BNaN
Cundefined
DError: handleClick is not a function
Attempts:
2 left
💡 Hint

Remember how arguments are passed directly in Vue event handlers.

📝 Syntax
intermediate
2:00remaining
Which option correctly passes an argument to the event handler?

In Vue 3, which of the following syntaxes correctly passes the argument 42 to the submitForm method when a button is clicked?

A<button @click="submitForm{42}">Submit</button>
B<button @click="submitForm(42)">Submit</button>
C<button @click="submitForm=>42">Submit</button>
D<button @click="submitForm:42">Submit</button>
Attempts:
2 left
💡 Hint

Vue event handlers use parentheses to pass arguments.

🔧 Debug
advanced
2:00remaining
Why does this handler not receive the argument?

Given this Vue 3 component snippet, why does handleClick receive undefined instead of the expected number?

Vue
<template>
  <button @click="handleClick">Click me</button>
</template>

<script setup>
function handleClick(num) {
  console.log(num)
}
</script>
ABecause the function is missing an arrow syntax, it cannot receive arguments.
BBecause the function is not declared with ref(), it cannot receive arguments.
CBecause Vue automatically passes the event object, num should be the event, not undefined.
DBecause the handler is called without parentheses, no argument is passed, so num is undefined.
Attempts:
2 left
💡 Hint

Think about how Vue calls event handlers without explicit arguments.

state_output
advanced
2:00remaining
What is the value of count after clicking the button twice?

Analyze this Vue 3 component. What will be the value of count after clicking the button two times?

Vue
<template>
  <button @click="increment(2)">Add 2</button>
  <p>{{ count }}</p>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(0)

function increment(amount) {
  count.value += amount
}
</script>
A0
B2
C4
DNaN
Attempts:
2 left
💡 Hint

Each click adds 2 to count. Two clicks add 4 total.

🧠 Conceptual
expert
2:00remaining
Which option correctly passes both the event and a custom argument to the handler?

In Vue 3, you want to pass the native event object and a custom argument id to your handler handleClick. Which option achieves this correctly?

A<button @click="event => handleClick(event, id)">Click</button>
B<button @click="handleClick($event, id)">Click</button>
C<button @click="handleClick(id, $event)">Click</button>
D<button @click="handleClick(id)">Click</button>
Attempts:
2 left
💡 Hint

Use an inline arrow function to pass both event and custom arguments.