其他分享
首页 > 其他分享> > Vue3 使用 setup 语法在 v-for 循环中保存 ref 数组

Vue3 使用 setup 语法在 v-for 循环中保存 ref 数组

作者:互联网

<template>
  <div>
    <span v-for="item in content" :ref="setItemRef"> {{ item }}</span>
  </div>
</template>

<script setup>
import { onBeforeUpdate, ref } from 'vue'

const refList = ref([])

const setItemRef = (el) => {
  refList.value.push(el)
}

// 更新前需置空
onBeforeUpdate(() => {
  refList.value = []
})

</script>

标签:el,refList,setup,onBeforeUpdate,value,Vue3,const,ref
来源: https://www.cnblogs.com/bpone/p/16275866.html