在 Vue 2 中,在 v-for 里使用的 ref attribute 會用 ref 數組填充相應的 $refs property。當存在嵌套的 v-for 時,這種行為會變得不明確且效率低下。 在 Vue 3 中,這樣的用法將不再在 $ref 中自動創建數組。要從單個綁定獲取多個 ref,請將 ref 綁定到一個更靈活的函數上 (這是一個新特性)
<div v-for="(item,index) in list" ref="setItemRef"></div>
v2中 setItemRef 會自動填充為數組,可以通過 this.$refs.setItemRef[index]來使用。
v3中 需要手動綁定ref的值,如下,實際開發中setItemRef應該由list 遍歷時動態生成
<div v-for="(item,index) in list" :ref="setItemRef[index]"></div>
data{
return {
list:[{
id:1,
name:'name1'
},
{
id:2,
name:'name2'
}
],
setItemRef:['listOne','listTwo']
}
}