vue进入页面自动获取input框焦点
程序员文章站
2024-02-10 14:08:40
...
template里面
<input v-focus type="text">
全局注册 我放在utils.js里面
import Vue from 'vue'
// 注册一个全局自定义指令 `v-focus`
Vue.directive('focus', {
// 当被绑定的元素插入到 DOM 中时……
inserted: function (el) {
// 聚焦元素
el.focus()
}
})
组件注册,与created、mounted同级的
directives: {
focus: {
// 指令的定义
inserted: function (el) {
el.focus()
}
}
}