vue3创建和获取循环动态ref

定义如下:

const boxRefs = ref([]);
const setRef = (el) => {
    
    
  if (el) {
    
    
    boxRefs.value.push(el);
  }
};

然后在需要循环赋值ref的元素上使用setRef,具体如下

                    <el-radio-button
                      :key="index"  
                      v-for="(item, index) in test"
                    >
                      <div
                        :ref="setRef"
                        width="145"
                        height="160"
                      ></div>
                    </el-radio-button>

此时每个元素的值便都存boxRefs数组中了,使用方法和使用数组无异,如下

boxRefs.value[1]

猜你喜欢

转载自blog.csdn.net/weixin_45807026/article/details/126806689