我有一个简单的 Angular 组件,在渲染时会抛出以下错误:
MyComponent.html:10 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
Previous value: 'ngForOf: [object Map Iterator]'.
Current value: 'ngForOf: [object Map Iterator]'.
Run Code Online (Sandbox Code Playgroud)
错误会抛出两次,即使移动到没有其他代码的最低限度的 Angular 项目(只是一个 AppComponent 包装这个组件)。
我将代码最小化到仍然重现问题的最小形式。我用硬编码的 observable 替换了数据服务。
MyComponent.html:10 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
Previous value: 'ngForOf: [object Map Iterator]'.
Current value: 'ngForOf: [object Map Iterator]'.
Run Code Online (Sandbox Code Playgroud)
请注意,asyncPipe
在这种情况下使用更常见 - 但我不能使用它。
@Component({
selector: 'my-component',
templateUrl: './my.component.html',
styleUrls: ['./my.component.scss'],
})
export class MyComponent implements OnInit, OnDestroy {
private cobs: Map<number, string>;
private cobSub: Subscription;
constructor() {}
ngOnInit() {
// originally data comes from a service...
this.cobSub = of(new Map<number, string>([[1, 'a'], [2, 'b']]))
.subscribe({
next: cobs => { this.cobs = cobs; },
});
}
ngOnDestroy(): void {
this.cobSub.unsubscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么会出现问题?
如何修复?
您在 Map 上迭代的方式有问题,您可以尝试:
export class AppComponent implements OnInit {
private cobs = new Map<number, string>();
private cobSub: Subscription;
constructor() {}
ngOnInit() {
this.cobSub = of(new Map<number, string>([[1, 'a'], [2, 'b']]))
.subscribe( cobs => {
this.cobs = cobs;
});
}
getKeys(map){
return Array.from(map.keys()); // add it
}
ngOnDestroy(): void {
this.cobSub.unsubscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
在 html 中:
<div>
<table *ngIf="cobs">
<tr>
<th>Id</th>
<th>Class of Business</th>
</tr>
<tr *ngFor="let key of getKeys(cobs)">
<td>{{key}}</td>
<td>{{cobs.get(key)}}</td>
</tr>
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 Angular 6.1+ ,则可以使用默认管道键值:
<tr *ngFor="let key of cobs | keyvalue">
<td>{{key.key}}</td>
<td>{{key.value}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
441 次 |
最近记录: |