mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-05 19:41:42 +08:00
fix(@vant/use): allow to call useWindowSize outside setup (#9834)
This commit is contained in:
parent
79146db9d5
commit
a9ce9096e4
@ -1,6 +1,5 @@
|
|||||||
import { ref, Ref } from 'vue';
|
import { ref, Ref } from 'vue';
|
||||||
import { inBrowser } from '../utils';
|
import { inBrowser } from '../utils';
|
||||||
import { useEventListener } from '../useEventListener';
|
|
||||||
|
|
||||||
let width: Ref<number>;
|
let width: Ref<number>;
|
||||||
let height: Ref<number>;
|
let height: Ref<number>;
|
||||||
@ -10,16 +9,16 @@ export function useWindowSize() {
|
|||||||
width = ref(0);
|
width = ref(0);
|
||||||
height = ref(0);
|
height = ref(0);
|
||||||
|
|
||||||
const update = () => {
|
|
||||||
if (inBrowser) {
|
if (inBrowser) {
|
||||||
|
const update = () => {
|
||||||
width.value = window.innerWidth;
|
width.value = window.innerWidth;
|
||||||
height.value = window.innerHeight;
|
height.value = window.innerHeight;
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
update();
|
update();
|
||||||
useEventListener('resize', update);
|
window.addEventListener('resize', update, { passive: true });
|
||||||
useEventListener('orientationchange', update);
|
window.addEventListener('orientationchange', update, { passive: true });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { width, height };
|
return { width, height };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user