ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
生活随笔
收集整理的這篇文章主要介紹了
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
?
解決方案:
- 異步更新(建議使用)
- 強制進行變更檢測,但是會觸發(fā)子組件的變更檢測,再次導致父組件屬性改變
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
?
轉載于:https://www.cnblogs.com/wjw1014/p/11313167.html
總結
以上是生活随笔為你收集整理的ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到羊蹄子什么意思
- 下一篇: 女人梦到别人流产出血是怎么回事