dialog中的 el-input获取焦点

983 阅读1分钟

需要在dialog打开时候让input获取焦点,开发时发现用autofocus无法获取焦点,查阅资料后发现,需要在Dialog 打开的回调中,用$nextTick获取dom,调用focus方法获取

<el-dialog
  title="发卡"
  :visible.sync="customerVisible"
  @open="customerDialogOpen"  // 这个是重点
>
  <el-input  ref="customerInput"   ></el-input>
</el-dialog>
//销售员 dialog 打开时 获取焦点
  customerDialogOpen() {
    this.customerVisible = true
    this.$nextTick(function () {
      this.$refs.customerInput[0].focus();
    });
  },