useRefs.ts 542 B

123456789101112131415161718192021222324
  1. import type { ComponentPublicInstance, Ref } from 'vue';
  2. import { onBeforeUpdate, shallowRef } from 'vue';
  3. function useRefs<T = HTMLElement>(): {
  4. refs: Ref<T[]>;
  5. setRefs: (index: number) => (el: Element | ComponentPublicInstance | null) => void;
  6. } {
  7. const refs = shallowRef([]) as Ref<T[]>;
  8. onBeforeUpdate(() => {
  9. refs.value = [];
  10. });
  11. const setRefs = (index: number) => (el: Element | ComponentPublicInstance | null) => {
  12. refs.value[index] = el as T;
  13. };
  14. return {
  15. refs,
  16. setRefs,
  17. };
  18. }
  19. export { useRefs };