我注意到在Vue 3的组合式API
例如,在Vue 3文档中的示例:
但是我做不到,因为在Vue3中,我需要让变量名和ref属性值相似。感谢您提供的任何帮助。
<script setup>
中创建模板引用时,需要使用与ref值完全相同的变量名。例如,在Vue 3文档中的示例:
<template>
<div ref="root">This is a root element</div> <--- notice the ref name "root"
</template>
<script>
import { ref, onMounted } from 'vue'
export default {
setup() {
const root = ref(null) <---- and the variable name should be "root" too
onMounted(() => {
console.log(root.value)
})
return {
root
}
}
}
</script>
我想知道我是否可以像Vue 2一样使用ref,因为我的元素的ref是随机字符串,在Vue 2中只需将名称传递到$refs对象即可。
<div ref="myCustomRefName"></div>
<script>
let myRef = this.$refs["myCustomRefName"]
</script>
但是我做不到,因为在Vue3中,我需要让变量名和ref属性值相似。感谢您提供的任何帮助。
refs
的单个对象,并通过键访问它。Refs 可以使用 ref 函数分配给您需要的任何对象。它们也可以在 getCurrentInstance() 上访问,但这不应该是第一选择。 - Estus Flask<Modal ref="something"/>
,并且我想通过ref动态获取模态框组件。它应该像这样$modal.open('something')
。为此,我认为我需要像Vue 2一样获取refs对象。 - zuramai