程序员

vue3 ts 动态获取 dom 对象

2022-11-02  本文已影响0人  wcmszgdqm

如果 dom 对象是动态赋予 id, 一般在循环中生成,例如

<template v-for="item in list">
    <input :ref="item.id"  type="text" />
</template>

在 vue2 时,可以通过 this.$refs 在集合中获取 dom 对象,vue3 也可以使用相同的方法处理,伪代码如下

<script setup lang="ts">
import { getCurrentInstance, reactive, ref } from "vue"

const { proxy } = getCurrentInstance() as any

// index 为动态索引,list[index].id 为动态获取的 ref 
proxy.$refs[list[index].id][0].focus()
上一篇 下一篇

猜你喜欢

热点阅读