避免引用未绑定的方法,这可能会导致无意中确定“this”的范围

Von*_*tos 9 angular

您好,关于如何预防的任何想法

错误避免引用未绑定的方法,这可能会导致无意中确定this @typescript-eslint/unbound-method的范围

16:41  error  Avoid referencing unbound methods which may cause unintentional scoping of `this`  @typescript-eslint/unbound-method
  16:62  error  Avoid referencing unbound methods which may cause unintentional scoping of `this`  @typescript-eslint/unbound-method
  17:34  error  Avoid referencing unbound methods which may cause unintentional scoping of `this`  @typescript-eslint/unbound-method
  18:35  error  Avoid referencing unbound methods which may cause unintentional scoping of `this`  @typescript-eslint/unbound-method
  18:56  error  Avoid referencing unbound methods which may cause unintentional scoping of `this`  @typescript-eslint/unbound-method
  20:40  error  Avoid referencing unbound methods which may cause unintentional scoping of `this`  @typescript-eslint/unbound-method
  21:46  error  Avoid referencing unbound methods which may cause unintentional scoping of `this`  @typescript-eslint/unbound-method

Run Code Online (Sandbox Code Playgroud)
import { Component, OnInit } from '@angular/core';
import { AbstractControl, FormControl, FormGroup, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {
  registrationForm: FormGroup;

  constructor() { }

  ngOnInit() {
    this.registrationForm = new FormGroup({
      companyName: new FormControl('', [Validators.required, Validators.email]),
      name: new FormControl('', [Validators.required]),
      email: new FormControl('', [Validators.required, Validators.email]),
      password: new FormGroup({
        password: new FormControl('', [Validators.required, Validators.minLength(8)]),
        repeatPassword: new FormControl('', [Validators.required])
      }, {validators: [this.passwordMismatchValidator()]})
    });
  }

  onRegistrationFormSubmit() {
    if (!this.registrationForm.valid) {
      return;
    }
  }

  passwordMismatchValidator(): ValidatorFn {
    return (c: AbstractControl): ValidationErrors | null => {
      const password = c.get('password')?.value;
      const repeatPassword = c.get('repeatPassword')?.value;

      if (password !== repeatPassword) {
        return {'passwordMismatch': true};
      }

      return null;
    };
  }
}

Run Code Online (Sandbox Code Playgroud)

Mic*_*ael 17

看到这个问题。看来您必须执行以下操作之一:

  • Angular 库从静态方法更改为静态箭头函数属性。
  • Angular 库用类似于this: void我们可以在此规则中构建处理的内容来注释其函数。
  • 您,用户,禁用评论。

目前,我想唯一的解决方案是在行/文件/项目级别完全禁用规则,或者将严重性从错误更改为简单警告。

对于前一种情况,只需添加以下内容:

在后一种情况下,请将以下内容放入ruleseslint 配置文件的部分(即.eslintrc.json)(以在整个项目中显示警告而不是错误):

"@typescript-eslint/unbound-method": "warn"
Run Code Online (Sandbox Code Playgroud)

编辑:对于 Angular Validators.*,您也可以覆盖 eslint 配置文件中的规则(请参阅文档):

"@typescript-eslint/unbound-method": [
    "error",
    {
        "ignoreStatic": true
    }
],
Run Code Online (Sandbox Code Playgroud)

  • `"ignoreStatic": true` 似乎是目前最好的解决方案。 (4认同)