--- title: Use .once Modifier for Single-Use Event Handlers impact: LOW impactDescription: The .once modifier auto-removes event listeners after first trigger, preventing repeated handler calls type: best-practice tags: [vue3, events, modifiers, once, event-handling] --- # Use .once Modifier for Single-Use Event Handlers **Impact: LOW** - Vue provides a `.once` modifier for event listeners that automatically removes the listener after it fires once. This is useful for one-time events like initialization callbacks, first-interaction tracking, or one-time animations. ## Task Checklist - [ ] Use `.once` for events that should only fire once - [ ] Consider `.once` for analytics first-interaction tracking - [ ] Use `.once` for initialization events - [ ] Remember `.once` works on both native and component events ## Basic Usage **Component events:** ```vue ``` **Native DOM events:** ```vue ``` ## Common Use Cases ### One-Time Initialization ```vue ``` ### First Interaction Analytics ```vue ``` ### Lazy Loading Trigger ```vue ``` ### One-Time Animation ```vue ``` ## Combining with Other Modifiers ```vue ``` ## Equivalent Manual Implementation Without `.once`, you'd need to manually track and remove: ```vue ``` ## When NOT to Use .once Don't use `.once` when: - You need the event to fire multiple times - You want to conditionally allow repeated fires - The "once" logic is complex (use manual ref tracking instead) ```vue ``` ## Reference - [Vue.js Event Handling - Event Modifiers](https://vuejs.org/guide/essentials/event-handling.html#event-modifiers) - [Vue.js Component Events](https://vuejs.org/guide/components/events.html)