简体   繁体   English

ExpressionChangedAfterItHasBeenCheckedError原因

[英]ExpressionChangedAfterItHasBeenCheckedError cause

I have the follow design: 我有以下设计:

Parent
+ Child 1
  + Child 2

Child 2 contains and input field, the user hits ENTER and it emits an event. 子项2包含和输入字段,用户单击ENTER并发出事件。 Child 1 intercepts it and emits an execute event which is intercepted by the Parent. 子级1对其进行拦截并发出执行事件,该事件被父级拦截。 The Parent executes a search and sends the results back to Child 1 via an Input(). 父级执行搜索,并将结果通过Input()发送回子级1。

In one scenario when the user navigates to the Parent with a reload=Y flag the search is run automatically based on the previously saved criteria. 在一种情况下,当用户使用reload = Y标志导航到父项时,将根据先前保存的条件自动运行搜索。 I do this in ngOnInit by checking the route params reload=Y and then emitting the same execute event, it goes to the parent and the results come back via Input(), this is working however I also get this error in the console - 我在ngOnInit中执行此操作,方法是检查路由参数reload = Y,然后发出相同的execute事件,它传递给父对象,结果通过Input()返回,这是可行的,但是我也在控制台中收到此错误-

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 Previous value: 'undefined'. 上一个值:“未定义”。 Current value: '[object Object]'. 当前值:“ [object Object]”。

I followed this article: https://blog.angularindepth.com/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error-e3fd9ce7dbb4 我关注了这篇文章: https : //blog.angularindepth.com/everything-you-need-to-know-about-the-expressionchangedafterithasenencheckederror-error-e3fd9ce7dbb4

and used a setTimeout() as described and now the error is gone. 并按照说明使用了setTimeout(),现在错误消失了。 What exactly happened here? 这里到底发生了什么?

What exactly is setTimeout doing. setTimeout到底在做什么。 The official explanation is "The setTimeout function schedules a "macrotask" which will be executed in the following VM turn". 官方解释是“ setTimeout函数安排了一个“宏任务”,它将在下一个VM回合中执行”。

So setTimetout delays the execution of the defined function until the next change detection cycle? 那么setTimetout将定义的函数的执行延迟到下一个更改检测周期? What is meant by "next VM turn"? “下一个VM转”是什么意思?

By using the SetTimeout you're basically branching part of your flow into a new change detection cycle. 通过使用SetTimeout,您基本上会将流程的一部分分支到新的变更检测周期中。 The one that will happen after completing the task. 完成任务后将发生的一种。

The error you are seeing is basically a defence mechanism from Angular to prevent multiple sources updating the same property several times in the same change detection cycle. 您看到的错误基本上是Angular的防御机制,可防止多个源在同一更改检测周期中多次更新同一属性。 If this was allowed it would be really tricky to figure out the final value displayed and who produced it. 如果允许的话,要弄清楚显示的最终值和产生它的人真的很棘手。

That's the reason you no longer see the error, because the multiple changes will no longer be done on the same change detection cycle. 这就是您不再看到该错误的原因,因为将不再在同一更改检测周期上进行多个更改。 You are taking ownership of splitting them into different timings so Angular is fine with that 您拥有将它们分为不同时间的所有权,因此Angular可以

暂无
暂无

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

相关问题 角度2管道绑定导致ExpressionChangedAfterItHaHasBeenCheckedError - Angular 2 Pipe Binding Cause ExpressionChangedAfterItHasBeenCheckedError 动态添加元素中的元素引用导致 ExpressionChangedAfterItHasBeenCheckedError: - Element reference in dynamically added element cause ExpressionChangedAfterItHasBeenCheckedError: ExpressionChangedAfterItHasBeenCheckedError - ExpressionChangedAfterItHasBeenCheckedError 为什么更改NgOninit上的父级数据不会导致ExpressionChangedAfterItHaHasBeenCheckedError? - Why does changing a parents data on NgOninit not cause ExpressionChangedAfterItHasBeenCheckedError? Angular 具有未定义可观察对象的应用程序不会导致 ExpressionChangedAfterItHasBeenCheckedError 错误,但它似乎应该? - Angular app with undefined observable does not cause ExpressionChangedAfterItHasBeenCheckedError Error but it seems like it should? Angular 8 到 11 是否发生了一些变化,导致子与父通信抛出 ExpressionChangedAfterItHasBeenCheckedError - Did something change with Angular 8 to 11 to cause Child to Parent communication to throw ExpressionChangedAfterItHasBeenCheckedError ngIf ExpressionChangedAfterItHasBeenCheckedError - ngIf ExpressionChangedAfterItHasBeenCheckedError 获取ExpressionChangedAfterItHasBeenCheckedError - Getting ExpressionChangedAfterItHasBeenCheckedError ExpressionChangedAfterItHasBeenCheckedError解决方法 - ExpressionChangedAfterItHasBeenCheckedError workaround ExpressionChangedAfterItHasBeenCheckedError 解释 - ExpressionChangedAfterItHasBeenCheckedError Explained
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM