自定义指令之光标定位到输入框

发布时间 2023-04-11 14:20:32作者: 给天使看的戲

下面是一个自定义指令的例子,当一个 input 元素被 Vue 插入到 DOM 中后,它会被自动聚焦。

解决方案:在input标签里增加ref属性,然后在export default 中增加构子函数mounted (挂载完成),写入以下代码,表示页面渲染完成后光标定位输入框。

mounted(){
        this.$nextTick(() =>{
            this.$refs.inputName.focus()
        })
    }