我希望你能幫助我解決這個錯誤。
Chrome 資源管理器列印此跟蹤:
error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
39 <div ngSwitch="{{e%2}}">
這是我的“app.component.html”:
<mat-list role="list">
<mat-list-item role="listitem" *ngFor="let e of lista; let i = index">
{{i 1}}::{{e}} -
<div ngSwitch="{{e%2}}">
<div *ngSwitchCase="0" [ngClass]="'impar'">impar</div>
<div *ngSwitchCase="1" [ngClass]="'par'">par</div>
<div *ngSwitchDefault="0">Sin resultados.</div>
</div>
<button mat-stroked-button color="accent" (click)="remover(i)">Remover</button>
<mat-divider></mat-divider>
</mat-list-item>
</mat-list>
附加資訊:
- TS:“~4.5.2”
- 角 CLI:13.1.2
- 節點:16.13.1
- 包管理器:npm 8.1.2
- 作業系統:win32 x64
OBS:如果我用加號 ( ) 替換模運算子 (%),它編譯令人滿意。
uj5u.com熱心網友回復:
雖然lista問題中沒有提供的型別,但它似乎是string[]型別,可能是這樣的:
lista = ['0', '1']
如果是這種情況,那么在嘗試執行時會出錯{{e%2},因為不能在字串運算元上使用模數運算子。其他運算子(如-, )的情況也是如此*。
現在為什么 有效,因為當與字串一起使用時,它執行連接。
您可以通過將插值撰寫為 來克服該錯誤{{ e%2}},前提是它始終是字串中的有效數字,否則您將得到NaN.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/400351.html
