gpt4 book ai didi

javascript - ChangeDetectionStrategy.OnPush 什么时候实际运行变更检测?

转载 作者:太空狗 更新时间:2023-10-29 17:47:57 26 4
gpt4 key购买 nike

我正在构建一个包含许多组件的 Angular 4 应用程序,其中 ChangeDetectionStrategyOnPush。同时the Angular Documentation on the matter没有很多信息,各种消息来源说 OnPush 组件仅在其 @Input 更改(新对象或基元)时更新。

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

对于组件内部的事件,ChangeDetectionStrategy.OnPush 的具体规则是什么?

最佳答案

This blog postAngular University包含有关使用 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 上的“Change Detection on 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.

关于javascript - ChangeDetectionStrategy.OnPush 什么时候实际运行变更检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48068763/

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