gpt4 book ai didi

javascript - Angular 变化检测zone.js

转载 作者:行者123 更新时间:2023-12-03 01:53:09 24 4
gpt4 key购买 nike

对 Angular2+ 中有关 zone.js 的文章有疑问。我正在读一篇article from blog.angular-university.io 。我认为有些部分有点神秘,并且对此有一些疑问,并且我不确定我是否理解正确。文章指出:

A zone is nothing more than an execution context that survives multiple Javascript VM execution turns.

上述语句是否意味着堆叠在事件队列中的多个异步回调具有一个执行上下文,因此 this 的值相同?

It's a generic mechanism which we can use to add extra functionality to the browser. Angular uses Zones internally to trigger change detection.

Angular 正在运行时更改async 函数(settimeout、AJAX 等)。这是否意味着除了完成回调的回调之外,Angular 还具有用于检测更改的附加功能

我目前的理解正确吗?如果不是,那是哪里出了问题?

最佳答案

Does the above statement mean that multiple async callbacks which are stacked in the event queue have one execution context and thus the same value for this?

没有。将执行上下文简单地(过于简单地)视为由在特定区域中运行的所有代码共享的对象。例如在引导过程中,当 Angular 模块初始化时,Angular 会创建( fork )一个名为“Angular”的区域。此外,它设置了属性 'isAngularZone':true 。您可以将其视为创建一个在其中设置新属性的对象。现在,这个属性将可供所有在 Angular 中运行(或计划运行)的代码使用,就像在 Angular 区域中运行的代码一样。但是,如果您在 Angular 区域之外运行代码,则该属性将不可访问。

Does this mean that besides finishing the callback of the callback also has additional functionality which Angular uses to detect changes

回调没有任何附加功能。这是 Zone.js 处理回调的方式,它提供了 Angular 的附加功能,例如确定何时启动更改检测周期。例如。当您在 setTimeout 函数中传递回调时,它会被 Zone.js 包装在另一个函数中,并替换您的回调。这是monkey patching API 。现在,当 JS VM 调用 Zone.js 包装的回调时,它会在实际调用回调方法之前执行各种操作。这些操作之一是通知回调的调用区域(在其中调度该回调)。在这里,如果该区域是“Angular ”,它将生成事件并启动更改检测周期。

关于javascript - Angular 变化检测zone.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50339051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com