0
0
Vueframework~20 mins

Event modifiers (prevent, stop, once) in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Event Modifiers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when clicking the button with @click.prevent?
Consider this Vue component snippet:
<button @click.prevent="increment">Click me</button>

methods: {
  increment() {
    console.log('Clicked!')
  }
}

What is the effect of the .prevent modifier here?
AThe click event's default action is prevented, but the increment method still runs logging 'Clicked!'.
BThe click event is stopped from propagating to parent elements, but the increment method does not run.
CThe increment method runs only once and then the button disables itself.
DThe click event triggers normally without any prevention or stopping.
Attempts:
2 left
💡 Hint
Think about what .prevent does to the event's default behavior.
component_behavior
intermediate
2:00remaining
What is the effect of @click.stop on nested elements?
Given this Vue template:
<div @click="outer">
  <button @click.stop="inner">Click me</button>
</div>

methods: {
  outer() { console.log('Outer clicked') },
  inner() { console.log('Inner clicked') }
}

What happens when the button is clicked?
ABoth 'Inner clicked' and 'Outer clicked' are logged because .stop does not affect propagation.
BOnly 'Inner clicked' is logged because .stop prevents the event from reaching the div.
COnly 'Outer clicked' is logged because .stop disables the button's click handler.
DNeither message is logged because the event is stopped before handlers run.
Attempts:
2 left
💡 Hint
Remember what event.stopPropagation() does.
state_output
advanced
2:00remaining
How many times does the method run with @click.once?
Look at this Vue component snippet:
<button @click.once="count++">Click me</button>

setup() {
  const count = ref(0)
  return { count }
}

If the user clicks the button 5 times, what is the final value of count?
A5
B0
CIt causes an error because .once is not valid here.
D1
Attempts:
2 left
💡 Hint
Think about what .once does to event listeners.
📝 Syntax
advanced
2:00remaining
Which option correctly uses multiple event modifiers on a click?
You want to prevent the default action and stop propagation on a button click in Vue. Which syntax is correct?
A&lt;button @click.stop.prevent="handleClick"&gt;Click&lt;/button&gt;
B&lt;button @click.preventOnce.stop="handleClick"&gt;Click&lt;/button&gt;
C&lt;button @click.prevent.stop="handleClick"&gt;Click&lt;/button&gt;
D&lt;button @click.once.prevent.stop="handleClick"&gt;Click&lt;/button&gt;
Attempts:
2 left
💡 Hint
Check the official Vue event modifier order and syntax.
🔧 Debug
expert
3:00remaining
Why does the @click.once handler not run more than once?
Given this Vue code:
<button @click.once="increment">Click</button>

setup() {
  const count = ref(0)
  function increment() {
    count.value++
  }
  return { count, increment }
}

After clicking the button multiple times, count only increases once. Why?
AThe .once modifier removes the event listener after the first click, so increment runs only once.
BThe count variable is reset after each click, so it never increases beyond 1.
CThe increment function has a bug preventing multiple increments.
DVue caches the event handler and ignores repeated clicks automatically.
Attempts:
2 left
💡 Hint
Recall what .once does to event listeners in Vue.