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.

typescript

API

Paramètres

Paramètres d'entrée acceptés par ce composable.

target
requis
TEventListenerTarget
The element to listen on. Omit to default to window. Accepts a plain element, a ref, or a template ref.
events
requis
TEventListenerEvents
One event name or an array of event names to register (e.g. "click" or ["mousedown", "touchstart"]).
listeners
requis
TEventListenerListeners
One handler or an array of handlers. Each listener is registered against every event name via a cross product.
options
TEventListenerOptions
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

vue

Listen on a specific element

vue