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>
相关推荐
map_3d_vis20 小时前
百度地图 Hello World:5 分钟用 JSAPI 4.0 显示一张地图
前端·javascript·地图·百度地图
不要试图纠正别人20 小时前
构建 0 报错,产物体积翻倍:换引擎最容易漏的对账
前端·javascript
恋猫de小郭21 小时前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
Hilaku21 小时前
面了 30 家公司拿到 2 个 Offer!
前端·javascript·程序员
延晗21 小时前
【从零开始 DeepSeek Harness 插件开发教程】三、从零开发 DeepSeek Harness 插件——完成第一个插件的挂载与运行
前端·人工智能
择势21 小时前
iOS 工程环境绝对隔离实践:开发机的"编不出"生产包
前端
某哈压力大21 小时前
Vue的双向绑定与自定义组件的v-model
前端·面试
CappuccinoRose1 天前
前端开发交互体系 - 汇总篇
开发语言·前端·javascript·css·html·交互
天天鸭1 天前
前端转全栈,要学会后端看项目的思维
前端·后端·ai编程
oil欧哟1 天前
AI Agent 常用操作入门:独立目录、Plan 模式与跨话题交接指南
前端