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.

typescript

API

Parameters

Input parameters accepted by this composable.

target
required
TEventListenerTarget
The element to listen on. Omit to default to window. Accepts a plain element, a ref, or a template ref.
events
required
TEventListenerEvents
One event name or an array of event names to register (e.g. "click" or ["mousedown", "touchstart"]).
listeners
required
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

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

vue

Listen on a specific element

vue