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.

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...