import{o as t,a,y as n}from"./vue-libs.b44bc779.js";const e={class:"van-doc-markdown-body"},l=n(`

useEventListener

Intro

Attaching an event when the component is mounted and activated, then removing the event when the component is unmounted and deactivated.

Usage

Basic Usage

import { ref } from 'vue';
import { useEventListener } from '@vant/use';

export default {
  setup() {
    // attach the resize event to window
    useEventListener('resize', () => {
      console.log('window resize');
    });

    // attach the click event to the body element
    useEventListener(
      'click',
      () => {
        console.log('click body');
      },
      { target: document.body }
    );
  },
};

API

Type Declarations

type Options = {
  target?: EventTarget | Ref<EventTarget>;
  capture?: boolean;
  passive?: boolean;
};

function useEventListener(
  type: string,
  listener: EventListener,
  options?: Options
): void;

Params

NameDescriptionTypeDefault Value
typeEvent typestring-
listenerCallback functionEventListener-
optionsOptionsOptions-

Options

NameDescriptionTypeDefault Value
targetTarget elementEventTarget | Ref<EventTarget>window
captureWhether to enable capturebooleanfalse
passiveif true, indicates that the listener will never call preventDefault()booleanfalse
`,8),d=[l],h={__name:"use-event-listener.en-US",setup(c,{expose:s}){return s({frontmatter:{}}),(o,i)=>(t(),a("div",e,d))}};export{h as default};