主题
useEventListener
轻松使用 EventListener。在已安装时使用 addEventListener 进行注册,在未安装时自动使用 removeEventListener 进行注册。
¥Use EventListener with ease. Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
用法
¥Usage
js
import { useEventListener } from '@vueuse/core'
useEventListener(document, 'visibilitychange', (evt) => {
console.log(evt)
})
你还可以传递一个 ref 作为事件目标,当你更改目标时,useEventListener
将取消注册先前的事件并注册新的事件。
¥You can also pass a ref as the event target, useEventListener
will unregister the previous event and register the new one when you change the target.
ts
import { useEventListener } from '@vueuse/core'
const element = ref<HTMLDivElement>()
useEventListener(element, 'keydown', (e) => {
console.log(e.key)
})
js
import { useEventListener } from '@vueuse/core'
const element = ref()
useEventListener(element, 'keydown', (e) => {
console.log(e.key)
})
vue
<template>
<div v-if="cond" ref="element">
Div1
</div>
<div v-else ref="element">
Div2
</div>
</template>
你还可以调用返回来注销监听器。
¥You can also call the returned to unregister the listener.
ts
import { useEventListener } from '@vueuse/core'
const cleanup = useEventListener(document, 'keydown', (e) => {
console.log(e.key)
})
cleanup() // This will unregister the listener.
请注意,如果你的组件也在 SSR(服务器端渲染)中运行,你可能会收到错误(如 document is not defined
),因为 DOM API(如 document
和 window
)在 Node.js 中不可用。为了避免这种情况,你可以将逻辑放在 onMounted
钩子中。
¥Note if your components also run in SSR (Server Side Rendering), you might get errors (like document is not defined
) because DOM APIs like document
and window
are not available in Node.js. To avoid that you can put the logic inside onMounted
hook.
ts
// onMounted will only be called in the client side, so it guarantees the DOM APIs are available.
onMounted(() => {
useEventListener(document, 'keydown', (e) => {
console.log(e.key)
})
})
类型声明
显示类型声明
typescript
interface InferEventTarget<Events> {
addEventListener: (event: Events, fn?: any, options?: any) => any
removeEventListener: (event: Events, fn?: any, options?: any) => any
}
export type WindowEventName = keyof WindowEventMap
export type DocumentEventName = keyof DocumentEventMap
export interface GeneralEventListener<E = Event> {
(evt: E): void
}
/**
* Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
*
* Overload 1: Omitted Window target
*
* @see https://vueuse.org/useEventListener
* @param event
* @param listener
* @param options
*/
export declare function useEventListener<E extends keyof WindowEventMap>(
event: Arrayable<E>,
listener: Arrayable<(this: Window, ev: WindowEventMap[E]) => any>,
options?: MaybeRefOrGetter<boolean | AddEventListenerOptions>,
): Fn
/**
* Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
*
* Overload 2: Explicitly Window target
*
* @see https://vueuse.org/useEventListener
* @param target
* @param event
* @param listener
* @param options
*/
export declare function useEventListener<E extends keyof WindowEventMap>(
target: Window,
event: Arrayable<E>,
listener: Arrayable<(this: Window, ev: WindowEventMap[E]) => any>,
options?: MaybeRefOrGetter<boolean | AddEventListenerOptions>,
): Fn
/**
* Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
*
* Overload 3: Explicitly Document target
*
* @see https://vueuse.org/useEventListener
* @param target
* @param event
* @param listener
* @param options
*/
export declare function useEventListener<E extends keyof DocumentEventMap>(
target: DocumentOrShadowRoot,
event: Arrayable<E>,
listener: Arrayable<(this: Document, ev: DocumentEventMap[E]) => any>,
options?: MaybeRefOrGetter<boolean | AddEventListenerOptions>,
): Fn
/**
* Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
*
* Overload 4: Explicitly HTMLElement target
*
* @see https://vueuse.org/useEventListener
* @param target
* @param event
* @param listener
* @param options
*/
export declare function useEventListener<E extends keyof HTMLElementEventMap>(
target: MaybeRefOrGetter<HTMLElement | null | undefined>,
event: Arrayable<E>,
listener: (this: HTMLElement, ev: HTMLElementEventMap[E]) => any,
options?: boolean | AddEventListenerOptions,
): () => void
/**
* Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
*
* Overload 5: Custom event target with event type infer
*
* @see https://vueuse.org/useEventListener
* @param target
* @param event
* @param listener
* @param options
*/
export declare function useEventListener<
Names extends string,
EventType = Event,
>(
target: MaybeRefOrGetter<InferEventTarget<Names> | null | undefined>,
event: Arrayable<Names>,
listener: Arrayable<GeneralEventListener<EventType>>,
options?: MaybeRefOrGetter<boolean | AddEventListenerOptions>,
): Fn
/**
* Register using addEventListener on mounted, and removeEventListener automatically on unmounted.
*
* Overload 6: Custom event target fallback
*
* @see https://vueuse.org/useEventListener
* @param target
* @param event
* @param listener
* @param options
*/
export declare function useEventListener<EventType = Event>(
target: MaybeRefOrGetter<EventTarget | null | undefined>,
event: Arrayable<string>,
listener: Arrayable<GeneralEventListener<EventType>>,
options?: MaybeRefOrGetter<boolean | AddEventListenerOptions>,
): Fn