个人技术分享

vue2 项目实现焦点聚焦

  • 新建 focus.js 文件夹
import Vue from 'vue'
// 插件对象(必须有 install 方法, 才可以注入到 Vue.use 中)
export default {
  install () {
    Vue.directive('fofo', {
      inserted (el) {
        el = el.querySelector('input')
        el.focus()
      }
    })
  }
}
  • vue 页面使用 v-fofo
<van-search v-model.trim="bulkGoodsNo" v-fofo label="货号: " placeholder="请输入货号" />