简介
- 我们一般用
ref
函数来获取DOM元素
使用步骤
- 使用
ref
函数创建容器 - 在需要获取的dom元素上写
ref
- dom元素保存在容器的
value
属性上
代码
<script setup>
import {ref,onMounted} from "vue"const inputRef = ref()
onMounted(() => {console.log(inputRef);inputRef.value && inputRef.value.focus() // 聚焦
})
</script><template>
<input ref="inputRef">
</template>
ref返回的对象