mirror of
				https://gitee.com/vant-contrib/vant.git
				synced 2025-10-31 03:22:08 +08:00 
			
		
		
		
	perf(@vant/use): reduce event listener of useWindowSize (#9658)
* perf(@vant/use): reduce event listener of useWindowSize * chore: update
This commit is contained in:
		
							parent
							
								
									499b8464f6
								
							
						
					
					
						commit
						2008982ff9
					
				| @ -1,18 +1,26 @@ | |||||||
| import { ref } from 'vue'; | import { ref, Ref } from 'vue'; | ||||||
| import { inBrowser } from '../utils'; | import { inBrowser } from '../utils'; | ||||||
| import { useEventListener } from '../useEventListener'; | import { useEventListener } from '../useEventListener'; | ||||||
| 
 | 
 | ||||||
| export function useWindowSize() { | let width: Ref<number>; | ||||||
|   const width = ref(inBrowser ? window.innerWidth : 0); | let height: Ref<number>; | ||||||
|   const height = ref(inBrowser ? window.innerHeight : 0); |  | ||||||
| 
 | 
 | ||||||
|   const onResize = () => { | export function useWindowSize() { | ||||||
|  |   if (!width) { | ||||||
|  |     width = ref(0); | ||||||
|  |     height = ref(0); | ||||||
|  | 
 | ||||||
|  |     const update = () => { | ||||||
|  |       if (inBrowser) { | ||||||
|         width.value = window.innerWidth; |         width.value = window.innerWidth; | ||||||
|         height.value = window.innerHeight; |         height.value = window.innerHeight; | ||||||
|  |       } | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|   useEventListener('resize', onResize); |     update(); | ||||||
|   useEventListener('orientationchange', onResize); |     useEventListener('resize', update); | ||||||
|  |     useEventListener('orientationchange', update); | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
|   return { width, height }; |   return { width, height }; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user