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
js
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 },
);
},
};Remove Event Listener
useEventListener will return a cleanup function,you can call it to remove the event listener.
js
import { ref } from 'vue';
import { useEventListener } from '@vant/use';
export default {
setup() {
const cleanup = useEventListener('resize', () => {
console.log('window resize');
});
cleanup();
},
};API
Type Declarations
ts
type Options = {
target?: EventTarget | Ref<EventTarget>;
capture?: boolean;
passive?: boolean;
};
function useEventListener(
type: string,
listener: EventListener,
options?: Options,
): () => void;Params
| Name | Description | Type | Default Value |
|---|---|---|---|
| type | Event type | string | - |
| listener | Callback function | EventListener | - |
| options | 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 |