useEventListener
Commons
stable
Registers one or more DOM event listeners on a target element (or window by default) and automatically removes them when the Vue scope is disposed. Supports reactive targets and options via watchers.
API
Signature
Signature TypeScript complète extraite du fichier du composable.
API
Paramètres
Paramètres d'entrée acceptés par ce composable.
- targetrequisTEventListenerTarget
- The element to listen on. Omit to default to window. Accepts a plain element, a ref, or a template ref.
- eventsrequisTEventListenerEvents
- One event name or an array of event names to register (e.g. "click" or ["mousedown", "touchstart"]).
- listenersrequisTEventListenerListeners
- One handler or an array of handlers. Each listener is registered against every event name via a cross product.
- optionsTEventListenerOptions
- Standard addEventListener options (capture, passive, once). Reactive — changing this ref re-registers the listeners.
API
Valeurs de retour
Propriétés et méthodes exposées par l'objet retourné par le composable.
- stop
- Calling stop() immediately removes all registered listeners and stops the internal watcher. Called automatically on scope dispose.
Usage
Exemples
Extraits de code prêts à coller.
Listen on window
Listen on a specific element