Skip to content
On this page

useTemplateRefsList

Category
Last Changed
7 months ago

Shorthand for binding refs to template elements and components inside v-for.

This function only works for Vue 3.x.

Demo

12345
Open the console to see the output

Usage

<template>
  <div v-for="i of 5" :key="i" :ref="refs.set"></div>
</template>

<script setup lang="ts">
import { onUpdated } from 'vue'
import { useTemplateRefsList } from '@vueuse/core'

const refs = useTemplateRefsList<HTMLDivElement>()

onUpdated(() => {
  console.log(refs)
})
</script>
<template>
  <div v-for="i of 5" :key="i" :ref="refs.set"></div>
</template>

<script setup lang="ts">
import { onUpdated } from 'vue'
import { useTemplateRefsList } from '@vueuse/core'

const refs = useTemplateRefsList<HTMLDivElement>()

onUpdated(() => {
  console.log(refs)
})
</script>

Type Declarations

export declare type TemplateRefsList<T> = T[] & {
  set(el: Object | null): void
}
export declare function useTemplateRefsList<T = Element>(): Readonly<
  Ref<Readonly<TemplateRefsList<T>>>
>
export declare type TemplateRefsList<T> = T[] & {
  set(el: Object | null): void
}
export declare function useTemplateRefsList<T = Element>(): Readonly<
  Ref<Readonly<TemplateRefsList<T>>>
>

Source

SourceDemoDocs

Contributors

Anthony Fu
bjym

Changelog

No recent changes
useTemplateRefsList has loaded