ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
?
解決方案:
- 異步更新(建議使用)
- 強制進行變更檢測,但是會觸發子組件的變更檢測,再次導致父組件屬性改變
Parent.Component.ts
@Component({selector:"app-parent" })export class ParentComponent implements OnInit,AfterViewInit {public text = "給子組件的信息";constructor(private cdr: ChangeDetectorRef){}ngOnInit(){}// 該方法缺點: 子組件多的情況下,不易控制。不建議使用 ngAfterViewInit(){this.cdr.detectChanges();} }
Child.Component.ts
@Component({selector:"app-child" })export class ChildComponent implements OnInit, AfterViewInit {@Input text;constructor(private parentComponent: ParentComponent){}ngOnInit(){}ngAfterViewInit() {// 異步更新兩種方式// 第一種setTimeout( ()=>{this.parentComponent.text="update message"},2000);// 第二種Promise.resolve(null).then( ()=> {this.parentComponent.text="update message"});} }
?
?
參考原文:https://blog.csdn.net/friend_ship/article/details/81773057
?