refactor(List): refactor with composition api

This commit is contained in:
chenjiahan
2020-08-28 09:53:16 +08:00
parent 67f418ab07
commit ecb319625b
2 changed files with 97 additions and 95 deletions

View File

@@ -9,7 +9,7 @@ import {
} from 'vue';
export function useGlobalEvent(
target: EventTarget,
target: Ref<EventTarget>,
event: string,
handler: EventListener,
passive = false,
@@ -18,23 +18,23 @@ export function useGlobalEvent(
let binded: boolean;
function add() {
if (binded || (flag && !flag.value)) {
if (binded || (flag && !flag.value) || !target.value) {
return;
}
on(target, event, handler, passive);
on(target.value, event, handler, passive);
binded = true;
}
function remove() {
if (binded) {
off(target, event, handler);
if (binded && target.value) {
off(target.value, event, handler);
binded = false;
}
}
if (flag) {
watch(() => {
watch(flag, () => {
flag.value ? add() : remove();
});
}