静态ref的获取与使用
<template>
<div ref="cptRef">...</div>
</template>
<script setup>
import { ref } from 'vue'
const cptRef = ref(null)
</script>
//动态ref
<template>
<el-button :ref="setItemRef">动态Ref</el-button>
</template>
<script setup>
import { ref } from 'vue'
const tableRef = ref(null)
const setItemRef = el => {
if (el) {
tableRef.value = el
}
}
</script>
//v-for设置ref及其使用
<template>
<component v-for="item in cptArr" :key="item.type" :ref="setItemRef" :is="item.type"></component>
</template>
<script setup>
import { ref } from 'vue'
const cptArr = [
{
type: 'imgCpt',
option: {}
},
{
type: 'advCpt',
option: {}
}
]
const itemRefs = []
const setItemRef = el => {
if (el) {
itemRefs.push(el)
}
}
itemRefs.forEach(item => {
})
</script>