微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-ChangeDetectionStrategy.OnPush何时实际运行更改检测?

我正在构建一个包含许多组件的Angular 4应用,其中ChangeDetectionStrategy是OnPush.尽管the Angular Documentation on the matter缺少很多信息,但各种消息来源都说OnPush组件仅在其@Inputs更改(新对象或基元)时更新.

但是,似乎OnPush组件内的各种事件也会触发更改检测.但是,我注意到其他人不会触发更改检测.

关于组件内部事件的ChangeDetectionStrategy.OnPush有哪些具体规则?

解决方法:

Angular UniversityThis blog post包含有关使用ChangeDetectionStrategy.OnPush时触发更改检测的事件的几种指示:

An OnPush change detector gets triggered in a couple of other
situations other than changes in component input() references, it also
gets triggered for example:

  • if a component event handler gets triggered
  • if an observable linked to the template via the async pipe emits a new value

他们添加了以下建议:

So if we remember to subscribe to any observables as much as possible
using the async pipe at the level of the template, we get a couple of
advantages:

  • we will run into much less change detection issue using OnPush
  • we will
    make it much easier to switch from the default change detection
    strategy to OnPush later if we need to
  • Immutable data and @input()
    reference comparison is not the only way to achieve a high performant
    UI with OnPush: the reactive approach is also an option to use OnPush
    effectively

a comment上出现关于disqus的“ Angular 2上的角度变化检测”问题之后,Viktor Savkin解释说:

When using OnPush detectors, then the framework will check an OnPush
component when any of its input properties changes, when it fires an
event, or when an observable fires an event.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐