Vue3+ts (nuxt3)使用ref获取dom元素(单个、多个)

直接用字符串的形式即可,不管是多个还是单个,多个会自动放到一个数组里面,而且还能被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>

猜你喜欢

转载自blog.csdn.net/m0_64130892/article/details/131610470