声纳给出“预期分配或函数调用”消息

uma*_*uma 0 typescript ngrx angular angular-reactive-forms

我在 NgRx 工作,收到此错误:

“预期有一个赋值或函数调用,但看到的是一个表达式。”

中的声纳问题this.sfForm.get('code')?.[this._mode ? 'disable' : 'enable']();

我不明白来自声纳的消息,也不明白这里要解决什么问题。我需要一些帮助来理解代码并解决问题。

<mat-form-field [formGroup]="sfForm">
  <input Input
         matInput
         (keydown.enter)="search($event.target.value)"
         [type]="''"
         formControlName="code"
         required>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
sfForm: FormGroup;
private _mode: boolean = true;
      
public set scanMode(value: boolean) {
  this._mode = value;
  this.sfForm.get('code')?.[this._mode ? 'disable' : 'enable']();
}
Run Code Online (Sandbox Code Playgroud)

Lio*_*owe 5

这是该行的细分:

this.sfForm.get('code') // get by the key "code"
?.                      // if `undefined` or `null`, stop here (see #1 below)
[                       // else, get prop by expression in [square brackets]
    this._mode ?        // if this._mode is truthy...
        'disable'       // that prop is 'disable'
        : 'enable'      // else, that prop is 'enable'
]                       // (see #2 below)
()                      // call the function identified by that prop (with 0 args)
Run Code Online (Sandbox Code Playgroud)

在更详细的代码中,它可能如下所示:

const code = this.sfForm.get('code')

if (code !== null && typeof code !== 'undefined') {
    let modeFunction

    if (this._mode) {
        modeFunction = code.disable
    } else {
        modeFunction = code.enable
    }

    modeFunction()
}
Run Code Online (Sandbox Code Playgroud)