TypeScript 回车触发聚焦 @keyup.enter.native

1.vue登录界面输入用户名回车自动聚焦密码框

用户框里放:@keyup.enter.native="submitForm('formInline')";

密码框里放::ref="password"

登录数据:

formInline: {
   userName: '',
   password: '',
}

回车绑定事件:

submitForm(formName: string): void {
      if (this.formInline.userName !== '' && this.formInline.password === '') {
        // 聚焦密码
        (this.$refs as HTMLFormElement).password.focus();
      }
      if (this.formInline.userName === '' && this.formInline.password !== '') {
        // 聚焦用户名
        (this.$refs as HTMLFormElement).userName.focus();
      }

      // 数据验证
      (this.$refs[formName] as HTMLFormElement).validate((valid: boolean) => {
        if (valid) {
          retrn true;
        }
      });
    },

2.@keyup.enter.native跟@keyup.enter区别

直接在根元素上绑定可以用@keyup.enter

如果使用的封装组件,例如element的el-input就必须使用@keyup.enter.native

原文地址:https://www.cnblogs.com/xiaoxiaomini/p/14158226.html