Skip to content

useEventListener

事件监听取消

源码

❣️view the code❣️

ts
import { onMounted, onUnmounted } from 'vue';
export function useEventListener<K extends keyof DocumentEventMap>(
  target: Document,
  event: K,
  callback: (event: DocumentEventMap[K]) => void,
  options?: boolean | AddEventListenerOptions
) {
  onMounted(() => target.addEventListener(event, callback, options));
  onUnmounted(() => target.removeEventListener(event, callback, options));
}

参数

参数类型说明默认值
targetDocumentDOM元素-
event<K extends keyof DocumentEventMap>监听的事件名-
callback(event: DocumentEventMap[K]) => void事件的回调函数-
optionsboolean | AddEventListenerOptions有关 listener 属性对象-