I'm using angular 10 with material, I got this error ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked

I'm using angular 10 with material, I got this error ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked

这是 angular html 代码,我认为问题是由调用我的视图的输入组件(应用程序产品)引起的enter image description here

此错误仅在开发模式下抛出,您不会在生产模式下看到它。 当您的输入值在 Angular 变更检测生命周期完成之前更改时,就会发生这种情况。

尽管据我所知它没有任何副作用,但您可能希望将子组件(在此特定示例中为应用程序产品)ChangeDetectionStrategy 更改为 OnPush,这将强制 Angular 仅在您的输入值之一发生变化时检查变化。

如果您正在更新 angular 的值,请更改 ngAfterViewInit() 中的侦听变量, 如果有,将该代码移动到 ngOnInit() 或在 ngAfterViewInit

中使用 setTimeout