Vue3 v-for绑定的dom获取ref为undefined

这是代码结构

<div class="playerInfo" 
    v-for="(item, index) in data.playersInfo" 
    :key="index" 
    :ref="el => {if(el)playersRef[index] = el}"
    :style="`left:${item.position[0]};top:${item.position[1]}`"
    @click="playerPassHighLight(index)"
>
    <div class="player-name" style="margin-bottom: 5px;">
        {{ item.nameCn }}
    </div>
    <div class="player-avatar">
       <img :src="item.img" alt="" style="width: 60px; height:60px; border-radius: 30px;" />
    </div>
</div> 

然后在console.log(playersRef.value, playersRef.value[0])的时候出现:

playersRef.value有数据,但是playersRef.value[0]为undefined

解决:

参考关于VUE中v-for循环的dom使用ref获取不到问题-CSDN博客

用nextTick再调用一次

onMounted(() => {
   getPassInfo()
   nextTick(()=> getPassInfo())
})

 完美解决~

 P.S. 如果把方法放到updated里面会一直请求数据,就没用updated

相关推荐

  1. vue3 form弹窗方式

    2024-03-24 18:46:02       13 阅读
  2. vue双向数据原理(v2、v3)

    2024-03-24 18:46:02       9 阅读
  3. Vue3: 获取元素DOM方法

    2024-03-24 18:46:02       20 阅读
  4. vue3ref在div上面有什么用

    2024-03-24 18:46:02       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 18:46:02       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 18:46:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 18:46:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 18:46:02       18 阅读

热门阅读

  1. trt | torch2trt的使用方式

    2024-03-24 18:46:02       19 阅读
  2. P7480 Reboot from Blue 线段树优化建图跑最短路

    2024-03-24 18:46:02       17 阅读
  3. 和neo4j相似的python工具

    2024-03-24 18:46:02       18 阅读
  4. 【2024-03-20】华为春招笔试三道编程题解

    2024-03-24 18:46:02       16 阅读
  5. C# System.Console.WriteLine的格式化输出

    2024-03-24 18:46:02       16 阅读
  6. C#面:简述可空类型

    2024-03-24 18:46:02       17 阅读
  7. 【2024-03-18】顺丰春招笔试两道编程题解

    2024-03-24 18:46:02       19 阅读
  8. 【串口开发】android 智能设备开发 知识笔记

    2024-03-24 18:46:02       21 阅读
  9. 学习笔记 | 微信小程序项目day06

    2024-03-24 18:46:02       22 阅读
  10. mysql基础02

    2024-03-24 18:46:02       15 阅读