Event modifiers help control how events behave in Vue. They make it easy to stop events from doing their default actions or bubbling up, and to run handlers only once.
Event modifiers (prevent, stop, once) in Vue
Introduction
Syntax
Vue
<element @event.modifier="handler"> // Examples: <button @click.prevent="submitForm">Submit</button> <div @click.stop="handleClick">Click me</div> <button @click.once="doOnce">Click once</button>
Modifiers come after the event name, separated by a dot.
You can combine multiple modifiers like @click.prevent.stop.
Examples
Vue
<button @click.prevent="submitForm">Submit</button>Vue
<div @click.stop="handleClick">Click me</div>Vue
<button @click.once="doOnce">Click once</button>Sample Program
This Vue component shows three examples:
- The form uses
@submit.preventto stop page reload. - The div uses
@click.stopto stop event bubbling. - The button uses
@click.onceto run the handler only once.
The message updates to show which event happened.
Vue
<template> <form @submit.prevent="onSubmit"> <button type="submit">Submit</button> </form> <div @click.stop="onDivClick" style="padding: 1rem; border: 1px solid #ccc; margin-top: 1rem;"> Click inside this box </div> <button @click.once="onClickOnce" style="margin-top: 1rem;">Click me once</button> <p>{{ message }}</p> </template> <script setup> import { ref } from 'vue' const message = ref('') function onSubmit() { message.value = 'Form submitted without page reload!' } function onDivClick() { message.value = 'Div clicked, event did not bubble!' } function onClickOnce() { message.value = 'Button clicked once!' } </script>
Important Notes
Use .prevent to stop default browser actions like form submission or link navigation.
Use .stop to prevent events from bubbling up to parent elements.
Use .once to make sure the event handler runs only the first time the event happens.
Summary
Event modifiers make event handling simpler and cleaner in Vue.
.prevent stops default browser behavior.
.stop stops event bubbling.
.once runs the handler only once.
Practice
1. What does the
.prevent event modifier do in Vue?easy
Solution
Step 1: Understand the purpose of
The.prevent.preventmodifier is used to stop the default action the browser takes when an event happens, like submitting a form or following a link.Step 2: Compare with other modifiers
.stopstops event bubbling, and.onceruns the handler once, so they do not match the behavior of.prevent.Final Answer:
Stops the default browser action for the event -> Option BQuick Check:
.preventstops default action [OK]
Hint: Remember: prevent stops default browser actions [OK]
Common Mistakes:
- Confusing .prevent with .stop
- Thinking .prevent runs handler once
- Assuming .prevent delays event
2. Which of the following is the correct syntax to use the
.once modifier on a button click event in Vue?easy
Solution
Step 1: Recall Vue event modifier syntax
Modifiers come after the event name separated by a dot, like@click.once.Step 2: Check each option's syntax
<button @click.once="handleClick">Click me</button>uses correct syntax.<button @once.click="handleClick">Click me</button>reverses modifier and event.<button v-on:once-click="handleClick">Click me</button>uses invalid directive.<button @click.once()="handleClick">Click me</button>adds parentheses incorrectly.Final Answer:
<button @click.once="handleClick">Click me</button> -> Option AQuick Check:
Correct modifier syntax = Click me [OK]
Hint: Event.modifier order is event then dot then modifier [OK]
Common Mistakes:
- Placing modifier before event name
- Adding parentheses after modifier
- Using invalid directive names
3. Consider this Vue template snippet:
What will happen when the button is clicked twice quickly?
<button @click.once.prevent="submitForm">Submit</button>
What will happen when the button is clicked twice quickly?
medium
Solution
Step 1: Analyze the modifiers
.onceand.prevent.onceensures the handler runs only once..preventstops the default browser action on that event.Step 2: Understand the combined effect on two clicks
On first click,submitFormruns and default action is prevented. On second click, handler does not run due to.once, so no submit occurs.Final Answer:
The form submits once, default action prevented once -> Option AQuick Check:
.onceruns once +.preventstops default [OK]
Hint: Once runs handler once; prevent stops default each time [OK]
Common Mistakes:
- Thinking .once allows multiple runs
- Assuming .prevent only works once
- Ignoring combined effect of modifiers
4. You have this Vue code:
Clicking the button triggers which handlers?
<div @click.stop="outerClick"> <button @click="innerClick">Click</button> </div>
Clicking the button triggers which handlers?
medium
Solution
Step 1: Understand
The.stopon the outer div click.stopmodifier on the outer div's click event stops the event from bubbling up beyond the div.Step 2: Trace event when button is clicked
Clicking the button triggersinnerClick. The event bubbles to the div, triggeringouterClick, but.stopprevents further bubbling beyond the div.Final Answer:
OnlyinnerClickruns;outerClickis stopped -> Option CQuick Check:
.stopstops bubbling beyond element [OK]
Hint: Stop stops bubbling beyond element with modifier [OK]
Common Mistakes:
- Thinking .stop stops inner handler
- Thinking .stop prevents the outer handler from running
- Confusing event bubbling direction
5. You want a button that submits a form only once, prevents default submission, and stops the click event from bubbling. Which Vue event modifiers should you use?
hard
Solution
Step 1: Identify required modifiers and order
You need.onceto run handler once,.preventto stop default form submission, and.stopto stop event bubbling. The correct order is event name then modifiers separated by dots.Step 2: Check each option's correctness
@click.once.prevent.stopuses correct syntax and includes all required modifiers. Other options miss one of the modifiers.Final Answer:
@click.once.prevent.stop -> Option DQuick Check:
Correct modifier order: event.once.prevent.stop [OK]
Hint: Use event.once.prevent.stop in that order [OK]
Common Mistakes:
- Changing modifier order incorrectly
- Using invalid modifier syntax
- Forgetting one of the needed modifiers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
