<div v-for="(item, index) in arr">
<son-componet :ref="'son'+index+'Ref'"/>
</div>
获取DOM节点方法:
this.$refs[`son${index}Ref`][0];
eval("this.$refs.son"+index+"Ref")[0]
不生效方法: this.$refs['son'+index+'Ref']
this.$refs.string也不生效,因为这种方法默认将string视作key值,不会当作变量去解析,
动态的ref无法生效。
注意点:模板字符串里面不要加“+”号,不可写成:this.$refs[`son+${index}+Ref`][0]; 模板字符串内部不能有运算符
需要注意的是,外层div包裹了一层v-for循环,通过refs拿到的dom节点,实际上是一个数组,没有加循环则是一个dom对象,有较大区别。