小编Eug*_*ene的帖子

How to set initial focus on input field on page load (with VueJS 3 using Composition API)

How can I set the focus on an HTML input field upon page loading while using the Composition API of VueJS 3? I have the following HTML:

<div>
  <input type="text" id="filter" v-model="filter">
</div>
Run Code Online (Sandbox Code Playgroud)

And have tried this in the setup() function, but that doesn't set the focus:

setup() {
  onMounted(() => {
    const filter_fld = document.getElementById('filter')
    filter_fld.focus()
  })
}
Run Code Online (Sandbox Code Playgroud)

I also tried using the below.
HTML:

<div>
  <input type="text" ref="filter_fld" v-model="filter">
</div>
Run Code Online (Sandbox Code Playgroud)

And in setup() function:

setup() {
  const filter_fld …
Run Code Online (Sandbox Code Playgroud)

input setfocus vuejs3 vue-composition-api

2
推荐指数
2
解决办法
439
查看次数

标签 统计

input ×1

setfocus ×1

vue-composition-api ×1

vuejs3 ×1