特殊情况下的 OnPush 变化检测

OnPush Change Detection for special cases

我用 ngrx 开发了一个 Angular 2 应用程序,我对 OnPush 变化检测感兴趣以获得更好的性能。我读了几篇关于它的文章,其中说:“如果一个组件只依赖于它的输入属性,并且它们是不可变的,那么当且仅当它的一个输入属性发生变化时,这个组件才能改变。”。因此,我有两个(简单的)关于 OnPush 的问题:

1) 如果我的组件混合了@Input() 属性而不是输入属性,OnPush 就没用了吗?

2) 对于只有属性(没有@Input)的组件,同样的问题。

提前致谢:)

OnPush 不影响组件内部状态。

如果你看一下 Victor Savkin 的excellent post,他特别提到

It is worth noting that a component can still have private mutable state as long as it changes only due to inputs being updated or an event being fired from within the component’s template. The only thing the OnPush strategy disallows is depending on shared mutable state. Read more about it here.

值得花时间在这个 post 以及他提到的同伴 post 上。