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
Full TypeScript signature sourced from the composable file.
API
Parameters
Input parameters accepted by this composable.
- targetrequiredTEventListenerTarget
- The element to listen on. Omit to default to window. Accepts a plain element, a ref, or a template ref.
- eventsrequiredTEventListenerEvents
- One event name or an array of event names to register (e.g. "click" or ["mousedown", "touchstart"]).
- listenersrequiredTEventListenerListeners
- 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
Return values
Properties and methods exposed by the composable return object.
- stop
- Calling stop() immediately removes all registered listeners and stops the internal watcher. Called automatically on scope dispose.
Usage
Examples
Ready-to-paste usage snippets.
Listen on window
Listen on a specific element