[英]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)
?.
#1: 对?.
解释?.
condition ? val1 : val2
#2: condition ? val1 : val2
解释condition ? val1 : val2
condition ? val1 : val2
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.