<div v-for="(item, index) in arr">
<son-componet :ref="'son'+index+'Ref'"/>
</div>
獲取DOM節(jié)點方法:
this.$refs[`son${index}Ref`][0];
eval("this.$refs.son"+index+"Ref")[0]
不生效方法: this.$refs['son'+index+'Ref']
this.$refs.string也不生效,因為這種方法默認將string視作key值,不會當作變量去解析,
動態(tài)的ref無法生效。
注意點:模板字符串里面不要加“+”號,不可寫成:this.$refs[`son+${index}+Ref`][0]; 模板字符串內(nèi)部不能有運算符
需要注意的是,外層div包裹了一層v-for循環(huán),通過refs拿到的dom節(jié)點,實際上是一個數(shù)組,沒有加循環(huán)則是一個dom對象,有較大區(qū)別。