Essayez ce qui suit et faites-moi savoir si c'est ce que vous essayez d'obtenir :)
<template>
<div>
<h1 ref="el1">Element 1</h1>
<h1 ref="el2">Element 2</h1>
<h1 ref="el3">Element 3</h1>
</div>
</template>
<script setup>
import { reactive, toRefs, ref } from 'vue';
const state = reactive({
el1: ref(),
el2: ref(),
el3: ref(),
});
const { el1, el2, el3 } = toRefs(state);
console.log(el1, el2, el3);
// ObjectRefImpl {_object: Proxy(Object), _key: 'el1', _defaultValue: undefined, __v_isRef: true}
// ObjectRefImpl {_object: Proxy(Object), _key: 'el2', _defaultValue: undefined, __v_isRef: true}
// ObjectRefImpl {_object: Proxy(Object), _key: 'el3', _defaultValue: undefined, __v_isRef: true}
</script>
edit : si vous voulez faire une boucle dynamique, essayez ce qui suit
<template>
<h1 v-for="el in refsArr" ref="el">{{ el._key }}</h1>
</template>
<script setup>
import { reactive, toRefs, ref } from 'vue';
const state = reactive({
el1: ref(),
el2: ref(),
el3: ref(),
});
const refsObj = toRefs(state);
const refsArr = []
for (const key in refsObj) {
refsArr.push(refsObj[key]);
}
</script>