Input box automatically focuses

In the process of writing code, when we perform some functional operations, there will be events and requirements that automatically gain focus, mostly used for comments, replies, comments, etc. At this time, you can use getCurrentInstance() ; in this method, we can directly obtain the position of the input box and use the native focus() method to implement it;

<a-textarea autoSize class="textarea" rows="2" v-model:value="commentContent" placeholder="请发表你的评论" ref="groupName" @blur="LoseFocus" />

//例如这个文本域,我们首先使用ref来定义一个名字 ref="groupName",

import { ref,getCurrentInstance } from "vue"; //在script中引入getCurrentInstance 这个方法

const { proxy } = getCurrentInstance(); //在下面结构赋值出proxy方法

//这个时候我们可以console一下proxy,来看一下他里面的方法是什么样子的,根据状况的不同来使用,

 proxy.$nextTick(() => {
      proxy.$refs.groupName.focus();
 });

//我这里直接可以获取到focus方法,所以直接用refs.名字 来获取到这个dom元素,给这个元素设置聚焦事件,
这样自动聚焦就完成了

Guess you like

Origin blog.csdn.net/weixin_42602736/article/details/130748476