vue3打开页面后文本框自动获得焦点

时间:2024-05-10 13:35:09

字符串写法

<script setup>
import { ref, onMounted } from 'vue'
import './index.css'

const input = ref(null)



onMounted(() => {
  input.value.focus()
})
</script>

<template>
  <div class="m-home-wrap">
    <input ref="input" />
    <div class="m-home-demo"></div>
  </div>
</template>

<style></style>

函数写法

<script setup>
import { ref, onMounted } from 'vue'
import './index.css'

const input = ref(null)

onMounted(() => {
  input.value.focus()
})
</script>

<template>
  <div class="m-home-wrap">
    <input
      :ref="
        (el) => {
          input = el
        }
      "
    />
    <div class="m-home-demo"></div>
  </div>
</template>

<style></style>

人工智能学习网站

https://chat.xutongbao.top