改为:refInput.value?.focus();
<script setup lang="ts">
import {onMounted, ref} from "vue";
// 优先定义ref对象,否则后面无法取值
const refInput = ref<HTMLElement | null>(null)
onMounted(() => {
if (refInput.value) {
console.log('refInput存在')
refInput.value?.focus();
}
})
</script>
文章介绍了在Vue中如何使用ref和onMounted钩子来检测refInput是否存在,并在存在时聚焦。

被折叠的 条评论
为什么被折叠?



