简体   繁体   English

ViewChild 与 Stepper ExpressionChangedAfterItHasBeenCheckedError Angular 8

[英]ViewChild with Stepper ExpressionChangedAfterItHasBeenCheckedError Angular 8

I'm trying to use ViewChild to get a child component's attribute for the Stepper [completed] property, but I can't figure out how to fix the "ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'completed: null'. Current value: 'completed: false'".我正在尝试使用 ViewChild 来获取 Stepper [已完成] 属性的子组件的属性,但我不知道如何修复“ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it is checked. Previous value: 'completed: null '。当前值:'已完成:false'”。

I followed this answer: Angular Material Stepper Component For Each Step我遵循了这个答案: Angular Material Stepper Component For Each Step

parent component .ts
@ViewChild(FirstStepComponent, {static: false}) firstStepComp: FirstStepComponent;

get firstStepCompleted() {
    return this.firstStepComp ? this.firstStepComp.completed : null;
}

parent component .html
<mat-step [completed]="firstStepCompleted">
  <app-first-step>
  </app-first-step>
</mat-step>

in the child component .ts
completed: boolean = false;

Before all I have to tell you that this error is thrown only in dev mode.首先我要告诉你这个错误只在dev模式下抛出。 If you call enableProdMode() when bootstrapping the app, it won't get thrown.如果在bootstrapping应用程序时调用enableProdMode() ,它不会被抛出。 This doesnt happens mainly in dev mode because after every save angular will run its rounds of changeDetection cycles and with it double checks the bindings of it weather if it has changed from the initial.这不会主要发生在dev模式中,因为在每次保存 angular 之后,它都会运行它的几轮changeDetection循环,并且如果它从初始状态发生变化,它会双重检查它的天气bindings

Key Idea : Anything that changes a bindings needs to go through a cycle of changeDetection关键思想:任何改变bindings事情都需要经过一个changeDetection循环

For furthermore elaboration on this issue please go through @drew moore's answer here .有关此问题的进一步详细说明,请在此处阅读 @drew moore 的回答。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM