Performance: Event modifiers (prevent, stop, once)
This concept affects how user interactions are handled and how many event listeners are active, impacting interaction responsiveness and memory usage.
<button @click.prevent.once="handleClick">Click me</button> methods: { handleClick() { // handler code } }
<button @click="handleClick">Click me</button> methods: { handleClick(event) { event.preventDefault(); // handler code } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual preventDefault and stopPropagation in handler | No extra DOM ops | 0 | 0 | [X] Bad |
| Using .prevent and .stop modifiers | No extra DOM ops | 0 | 0 | [OK] Good |
| Adding event listener without .once for single-use events | No extra DOM ops | 0 | 0 | [X] Bad |
| Using .once modifier to auto-remove listener | No extra DOM ops | 0 | 0 | [OK] Good |
.prevent event modifier do in Vue?.prevent.prevent modifier is used to stop the default action the browser takes when an event happens, like submitting a form or following a link..stop stops event bubbling, and .once runs the handler once, so they do not match the behavior of .prevent..prevent stops default action [OK].once modifier on a button click event in Vue?@click.once.<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.<button @click.once.prevent="submitForm">Submit</button>
.once and .prevent.once ensures the handler runs only once. .prevent stops the default browser action on that event.submitForm runs and default action is prevented. On second click, handler does not run due to .once, so no submit occurs..once runs once + .prevent stops default [OK]<div @click.stop="outerClick"> <button @click="innerClick">Click</button> </div>
.stop on the outer div click.stop modifier on the outer div's click event stops the event from bubbling up beyond the div.innerClick. The event bubbles to the div, triggering outerClick, but .stop prevents further bubbling beyond the div.innerClick runs; outerClick is stopped -> Option C.stop stops bubbling beyond element [OK].once to run handler once, .prevent to stop default form submission, and .stop to stop event bubbling. The correct order is event name then modifiers separated by dots.@click.once.prevent.stop uses correct syntax and includes all required modifiers. Other options miss one of the modifiers.Pass the test and this pattern moves unknown