diff --git a/packages/vant-use/src/useClickAway/README.md b/packages/vant-use/src/useClickAway/README.md index 39c83b36a..714215ac2 100644 --- a/packages/vant-use/src/useClickAway/README.md +++ b/packages/vant-use/src/useClickAway/README.md @@ -60,7 +60,7 @@ export default { ```ts function useClickAway( - target: Element | Ref, + target: Element | Ref, listener: EventListener, options?: Options ): void; diff --git a/packages/vant-use/src/useClickAway/index.ts b/packages/vant-use/src/useClickAway/index.ts index d2e039010..b36312dba 100644 --- a/packages/vant-use/src/useClickAway/index.ts +++ b/packages/vant-use/src/useClickAway/index.ts @@ -7,7 +7,7 @@ export type UseClickAwayOptions = { }; export function useClickAway( - target: Element | Ref, + target: Element | Ref, listener: EventListener, options: UseClickAwayOptions = {} ) { @@ -19,7 +19,7 @@ export function useClickAway( const onClick = (event: Event) => { const element = unref(target); - if (!element.contains(event.target as Node)) { + if (element && !element.contains(event.target as Node)) { listener(event); } }; diff --git a/packages/vant-use/src/useRect/README.md b/packages/vant-use/src/useRect/README.md index 58e3ced44..ba62af274 100644 --- a/packages/vant-use/src/useRect/README.md +++ b/packages/vant-use/src/useRect/README.md @@ -31,7 +31,7 @@ export default { ### 类型定义 ```ts -function useRect((Element | Window) | Ref): DOMRect; +function useRect((Element | Window) | Ref): DOMRect; ``` ### 返回值 diff --git a/packages/vant-use/src/useRect/index.ts b/packages/vant-use/src/useRect/index.ts index 5b0d4ab5f..e02b96af4 100644 --- a/packages/vant-use/src/useRect/index.ts +++ b/packages/vant-use/src/useRect/index.ts @@ -5,7 +5,7 @@ function isWindow(val: unknown): val is Window { } export const useRect = ( - elementRef: (Element | Window) | Ref + elementRef: (Element | Window) | Ref ) => { const element = unref(elementRef); @@ -23,7 +23,7 @@ export const useRect = ( }; } - if (element.getBoundingClientRect) { + if (element && element.getBoundingClientRect) { return element.getBoundingClientRect(); } diff --git a/packages/vant-use/src/useScrollParent/README.md b/packages/vant-use/src/useScrollParent/README.md index cf6ef674d..475258ea8 100644 --- a/packages/vant-use/src/useScrollParent/README.md +++ b/packages/vant-use/src/useScrollParent/README.md @@ -37,7 +37,9 @@ export default { ### 类型定义 ```ts -function useScrollParent(element: Ref): Ref; +function useScrollParent( + element: Ref +): Ref; ``` ### 参数 diff --git a/packages/vant-use/src/useScrollParent/index.ts b/packages/vant-use/src/useScrollParent/index.ts index 202c30d9d..1c546402b 100644 --- a/packages/vant-use/src/useScrollParent/index.ts +++ b/packages/vant-use/src/useScrollParent/index.ts @@ -39,7 +39,7 @@ function getScrollParent(el: Element, root: ScrollElement = window) { } export function useScrollParent(el: Ref) { - const scrollParent = ref(); + const scrollParent = ref(); onMounted(() => { if (el.value) {