直接用字符串的形式即可,不管是多个还是单个,多个会自动放到一个数组里面,而且还能被watch监视。
注意需要在挂载后才能拿到这些数据。
<template>
<div ref="out">
<div class="item" v-for="k in xxxx" :key="i" ref="itemRefs">{
{ k }}</div>
</div>
</template>
<script setup lang='ts'>
/**单个ref */
const out = ref<Element | null>(null)
/**ref数组 */
const itemRefs = ref<Element[]>([])
watch(itemRefs, (newVal) => {
console.log('ref数组更新啦', newVal);
}, { deep: true })
onMounted(() => {
console.log(out.value ,itemRefs.value);
})
</script>