From a9ce9096e428c5baed2ade9732863631f7580069 Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 11 Nov 2021 16:54:04 +0800 Subject: [PATCH] fix(@vant/use): allow to call useWindowSize outside setup (#9834) --- packages/vant-use/src/useWindowSize/index.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/vant-use/src/useWindowSize/index.ts b/packages/vant-use/src/useWindowSize/index.ts index 02230d56c..5a17ba64b 100644 --- a/packages/vant-use/src/useWindowSize/index.ts +++ b/packages/vant-use/src/useWindowSize/index.ts @@ -1,6 +1,5 @@ import { ref, Ref } from 'vue'; import { inBrowser } from '../utils'; -import { useEventListener } from '../useEventListener'; let width: Ref; let height: Ref; @@ -10,16 +9,16 @@ export function useWindowSize() { width = ref(0); height = ref(0); - const update = () => { - if (inBrowser) { + if (inBrowser) { + const update = () => { width.value = window.innerWidth; height.value = window.innerHeight; - } - }; + }; - update(); - useEventListener('resize', update); - useEventListener('orientationchange', update); + update(); + window.addEventListener('resize', update, { passive: true }); + window.addEventListener('orientationchange', update, { passive: true }); + } } return { width, height };