perf: useRefs函数接收泛型类型
parent
7aeba3367d
commit
ee56231f02
|
@ -1,16 +1,20 @@
|
|||
import type { Ref } from 'vue'
|
||||
import { onBeforeUpdate, ref } from 'vue'
|
||||
|
||||
export function useRefs(): [Ref<HTMLElement[]>, (index: number) => (el: HTMLElement) => void] {
|
||||
const refs = ref([]) as Ref<HTMLElement[]>
|
||||
function useRefs<T = HTMLElement>(): { refs: Ref<T[]>; setRefs: (index: number) => (el: T) => void } {
|
||||
const refs = ref([]) as Ref<T[]>
|
||||
|
||||
onBeforeUpdate(() => {
|
||||
refs.value = []
|
||||
})
|
||||
|
||||
const setRefs = (index: number) => (el: HTMLElement) => {
|
||||
const setRefs = (index: number) => (el: T) => {
|
||||
refs.value[index] = el
|
||||
}
|
||||
|
||||
return [refs, setRefs]
|
||||
return {
|
||||
refs,
|
||||
setRefs,
|
||||
}
|
||||
}
|
||||
export { useRefs }
|
||||
|
|
Loading…
Reference in New Issue