gpt4 book ai didi

javascript - Vue 3 refs 在渲染函数中未定义

转载 作者:行者123 更新时间:2023-12-03 06:45:19 25 4
gpt4 key购买 nike

我有一个简单的 Vue 组件,其根元素为 ref="divRef" .然而,在 onMounted函数, divRef.value返回未定义。任何帮助将不胜感激。

import { defineComponent, onMounted, ref, Ref, h } from "vue"

export default defineComponent({
setup(props, context) {
const divRef = ref() as Ref<HTMLElement>

onMounted(() => {
console.log(divRef.value) // undefined
})

return () => {
return h(
"div",
{
ref: "divRef"
},
"This is a div"
)
}
}
})

最佳答案

在您的 render函数,传递divRef本身,而不是字符串:

return h(
"div",
{
//ref: "divRef" // DON'T DO THIS
ref: divRef
},
"This is a div"
)

关于javascript - Vue 3 refs 在渲染函数中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62839300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com