【发布时间】:2021-01-15 09:43:45
【问题描述】:
我有一个非常简单的设置。
我在 app.component.html 中有一个输入字段,用户可以在其中输入字符串。我将该字符串添加到 Map
app.component.html -> 父组件
<div class="shopping-cart">
<app-shopping-cart-item [shoppingItems]="shoppingItems"></app-shopping-cart-item>
</div>
<p>
<button class="btn btn-primary" (click)="updateName()">Update Name</button>
</p>
app.component.ts
shoppingItems = new Map<string, number>();
updateName(){
this.shoppingItems.set('test', 1);
}
购物车组件.html
<div *ngFor="let article of this.shoppingItems.entries()" class="shopping-cart-item">
<div class="article-name">{{ article[0] }}</div>
</div>
Shopping-Cart-Item.component.ts
@Input()
shoppingItems;
问题是当我在输入值(在父项中)中输入一个值并单击更新时,它会更新子组件中显示的值。但我得到一个 ExpressionChangedAfterItHasBeenCheckedError。
我知道当我们没有单向数据流时会出现此错误。但我不明白为什么在这种情况下会出现此错误。
core.js:6210 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '[object Map Iterator]'. Current value: '[object Map Iterator]'.
at throwErrorIfNoChangesMode (core.js:8129)
at bindingUpdated (core.js:19991)
at Module.ɵɵproperty (core.js:21142)
at ShoppingCartItemComponent_Template (shopping-cart-item.component.html:3)
at executeTemplate (core.js:12059)
at refreshView (core.js:11906)
at refreshComponent (core.js:13358)
at refreshChildComponents (core.js:11635)
at refreshView (core.js:11958)
at refreshComponent (core.js:13358)
【问题讨论】:
-
你能显示完整的异常信息吗?
-
在问题中更新了
-
您是否在孩子中使用推送更改检测?
-
@bryan60 不...我没有
标签: javascript angular typescript angular-changedetection