ExpressionChangedAfterItHasBeenCheckedError in child component

前端 未结 1 1925
星月不相逢
星月不相逢 2021-01-02 08:00

I have a parent Component which updates a every second its array myValue. In a child component I want to create a chart which uses this array as data and update

相关标签:
1条回答
  • 2021-01-02 08:33

    You can find detailed explanations about that exception in this article. One technique to eliminate the exception is to force change detection with ChangeDetectorRef.detectChanges:

    export class ValueChartComponent implements AfterViewInit {
    
        constructor(private cd: ChangeDetectorRef) { }
    
        ngAfterViewInit(): void {
            ...
            this.cd.detectChanges();
        }
    
        ...
    }
    

    An alternative technique is to run the handler code asynchronously with setTimeout:

    export class ValueChartComponent implements AfterViewInit {
    
        ngAfterViewInit(): void {
            setTimeout(() => {
                ...
            });
        }
    
        ...
    }
    
    0 讨论(0)
提交回复
热议问题