mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat: add use-handler hook
This commit is contained in:
parent
6d4efa183e
commit
0d5b53d2a5
30
src/hooks/use-handler.ts
Normal file
30
src/hooks/use-handler.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { on, off } from '../utils';
|
||||
import { onMounted, onActivated, onUnmounted, onDeactivated } from 'vue';
|
||||
|
||||
export function useHandler(
|
||||
target: HTMLElement | Document | Window,
|
||||
event: string,
|
||||
handler: any,
|
||||
passive = false
|
||||
) {
|
||||
let added: boolean;
|
||||
|
||||
function add() {
|
||||
if (!added) {
|
||||
on(target, event, handler, passive);
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
|
||||
function remove() {
|
||||
if (added) {
|
||||
off(target, event, handler);
|
||||
added = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(add);
|
||||
onActivated(add);
|
||||
onUnmounted(remove);
|
||||
onDeactivated(remove);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user