import{o as t,a,y as n}from"./vue-libs.b44bc779.js";const e={class:"van-doc-markdown-body"},l=n(`
Attaching an event when the component is mounted
and activated
, then removing the event when the component is unmounted
and deactivated
.
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 }
);
},
};
type Options = {
target?: EventTarget | Ref<EventTarget>;
capture?: boolean;
passive?: boolean;
};
function useEventListener(
type: string,
listener: EventListener,
options?: Options
): void;
Name | Description | Type | Default Value |
---|---|---|---|
type | Event type | string | - |
listener | Callback function | EventListener | - |
options | Options | Options | - |
Name | Description | Type | Default Value |
---|---|---|---|
target | Target element | EventTarget | Ref<EventTarget> | window |
capture | Whether to enable capture | boolean | false |
passive | if true, indicates that the listener will never call preventDefault() | boolean | false |