0
0
Vueframework~20 mins

Method handlers vs inline handlers in Vue - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Event Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Difference in event handling output
Consider a Vue 3 component with a button. Which option correctly shows the output when clicking the button using a method handler vs an inline handler?
Vue
<template>
  <button @click="handleClick">Click me</button>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(0)
function handleClick() {
  count.value++
  console.log('Method count:', count.value)
}
</script>
AClicking the button logs 'Method count: NaN' after first click.
BClicking the button logs 'Method count: 1', then 'Method count: 2', etc.
CClicking the button logs 'Method count: 0' every time.
DClicking the button logs 'Method count: undefined' every time.
Attempts:
2 left
💡 Hint
Think about how the method updates the reactive count variable.
📝 Syntax
intermediate
2:00remaining
Correct inline event handler syntax in Vue 3
Which option shows the correct inline event handler syntax to increment a reactive count variable on button click?
Vue
<template>
  <button @click="???">Click me</button>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
A@click="count.value++"
B@click="count++"
C@click="() => count.value++"
D@click="incrementCount()"
Attempts:
2 left
💡 Hint
Remember that count is a ref and you must access its value.
🔧 Debug
advanced
2:00remaining
Why does this inline handler not update the UI?
Given this Vue 3 code, why does clicking the button not update the displayed count?
Vue
<template>
  <button @click="count++">Click me</button>
  <p>Count: {{ count }}</p>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
ABecause the button click event is not bound correctly.
BBecause the template cannot display ref variables directly.
CBecause <code>count++</code> does not update the ref's value property.
DBecause <code>count</code> is not initialized.
Attempts:
2 left
💡 Hint
Think about how refs work in Vue 3 and how to update them.
🧠 Conceptual
advanced
2:00remaining
Performance difference between method handlers and inline handlers
Which statement best explains the performance difference between method handlers and inline handlers in Vue 3?
AMethod handlers cause memory leaks while inline handlers do not.
BInline handlers are more performant because they are simpler and shorter.
CBoth have the same performance because Vue optimizes event handlers automatically.
DMethod handlers are more performant because they avoid creating a new function on each render.
Attempts:
2 left
💡 Hint
Consider how Vue treats inline functions during rendering.
state_output
expert
2:00remaining
State value after mixed method and inline handlers
Given this Vue 3 component, what is the value of count after clicking the 'Click me' button twice?
Vue
<template>
  <button @click="increment">Click me</button>
  <button @click="count.value++">Inline Increment</button>
  <p>Count: {{ count }}</p>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
  count.value += 2
}
</script>
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
The increment method adds 2 per click.