vueuse中的useTemplateRefsList

在 v-for 中绑定 ref 到模板元素和组件的简写方式

Demo1

html 复制代码
<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" />
</template>

Demo2

html 复制代码
<script setup lang="ts">
import { useTemplateRefsList } from '@vueuse/core'
import { nextTick, ref, watch } from 'vue'

const count = ref(5)
const refs = useTemplateRefsList<HTMLDivElement>()

watch(refs, async () => {
  await nextTick()
  console.log([...refs.value])
}, {
  deep: true,
  flush: 'post',
})
</script>

<template>
  <span v-for="i of count" :key="i" :ref="refs.set" class="mr-2">
    {{ i }}
  </span>
  <br>
  <button @click="count += 1">
    Inc
  </button>
  <button :disabled="count <= 0" @click="count -= 1">
    Dec
  </button>
  <note>Open the console to see the output</note>
</template>
相关推荐
懂懂tty9 分钟前
CRA 迁移 Rspack(实战)
前端·架构
小码哥_常1 小时前
Kotlin 助力 Android 启动“大提速”
前端
GreenTea2 小时前
AI 时代,工程师的不可替代性在哪里
前端·人工智能·后端
Jagger_2 小时前
能不能别再弄低代码害人了
前端
朦胧之2 小时前
AI 编程开发思维
前端·后端·ai编程
踩着两条虫2 小时前
VTJ:快速开始
前端·低代码·架构
木斯佳4 小时前
前端八股文面经大全:携程前端一面(2026-04-17)·面经深度解析
前端·状态模式
2301_799073024 小时前
基于 Next.js + 火山引擎 AI 的电商素材智能生成工具实战——字节跳动前端训练营成果
javascript·人工智能·火山引擎
Java后端的Ai之路4 小时前
LangChain ReAct Agent 核心技术问答
前端·react.js·langchain
码喽7号4 小时前
Vue学习七:MockJs前端数据模拟
前端·vue.js·学习