简体   繁体   English

声纳给出“预期的赋值或函数调用”消息

[英]Sonar gives 'Expected an assignment or function call' message

I work in NgRx and I receive this error:我在 NgRx 工作,但收到此错误:

'Expected an assignment or function call and instead saw an expression.' “期望赋值或函数调用,却看到了一个表达式。”

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

I don't understand the message from the Sonar, and what to fix here.我不明白来自声纳的消息,以及这里要解决的问题。 I need some help to understand the code and resolve the issue.我需要一些帮助来理解代码并解决问题。

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

Here's a breakdown of that line:这是该行的细分:

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)

In more verbose code, it might look like this:在更详细的代码中,它可能如下所示:

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()
}

If you mean to assign the labels, you cannot do that in this way.如果您要分配标签,则不能以这种方式进行。 when you do当你做

object[field]

like you did, you cannot assign values.像你一样,你不能赋值。

What you can do, is something like this :你可以做的是这样的:

this.sfForm.get('code')?.[this._mode] = this.sfForm.get('code')?.[this._mode] ? 'disable' : 'enable'

or in a shorter way if you want to put the field in a variable.或者以更短的方式,如果你想把字段放在一个变量中。

Also, note that you cannot call to functions inside of the '?'另外,请注意,您不能调用 '?' 内部的函数。 assigns, but only use statements.赋值,但只使用语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TSLint返回预期的赋值或函数调用 - TSLint returns expected an assignment or function call 未使用的表达式,需要赋值或 function 调用(角度) - Unused expression, expected an assignment or function call (Angular) 预期分配或 function 调用,而是看到一个表达式:no-unused-expressions - Expected an assignment or function call and instead saw an expression: no-unused-expressions TSLint在分配变量时返回预期的分配或函数调用 - TSLint returns expected an assignment or function call when assigning variables 未使用的表达式,期望赋值或函数调用承诺函数中的 Angular lint 错误 - unused expression, expected an assignment or function call Angular lint error in promise function 运行 tslint 时,角度单元测试中的“未使用的表达式,期望赋值或函数调用” - 'unused expression, expected an assignment or function call' in an angular unit test when running tslint 你能在用 typescript/angular 导出它之前创建一个 class 吗? | TSLint:未使用的表达式,需要赋值或 function 调用 - Can you create a class before exporting it in typescript/angular? | TSLint: unused expression, expected an assignment or function call Laravel-从Angular调用PHP函数以更新数据库会产生错误:期望表达 - Laravel- calling PHP function from Angular to update database gives error: Expression expected 解决此错误消息:请仔细检查项目密钥和声纳 url - Solve this error message : please double check the project key and sonar url 我有角模型类,具有变量和函数,所以当我调用函数时会给我错误 - I have class models in angular having variables and functions so when I call the function it gives me error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM