使用 Vue.js 2 api-composition,您可以.ref
在. 正如许多文章和此线程中所述:https ://github.com/vuejs/composition-api/issues/226它在 vue.js 3 中不可用,您应该声明与 ref 同名的属性-ed 元素:setupContext
setup()
<div ref='name'/>
setup() {
return {
name : ref(null)
}
}
但是,当您不知道 ref 名称时怎么办setup()
?
像这个“最小”的例子:
<div v-for="e in elements" @click="elements.push({name:Math.random(), content:Math.random()})">
<div :ref="e.name">e.content</div>
</div>
setup(){
return {
a_function_called_later(){
// I can use this.$refs but is there a more "vuejs3" way to do it ?
}
// ...???
}
}