首页 > 解决方案 > 变更检测策略在 Angular 中的工作原理

问题描述

如果我有多级组件,例如更改检测策略如何工作:

应用根组件

var data = {
 name:"ajai"
}

<app-root>
  <first-child [input]="data">
  </first-child>
</app-root>

First-Child-component.html

{{data}}
<second-child> </second-child>

在 first-child 组件中,我们将调用 second-child 作为子组件。

第一个子组件.ts

  @Input('data') data = {} 

如果我更改 App-component 中数据的值,它将重新渲染整个树。所以第一个子组件我正在使用更改检测策略 OnPush,因此如果从应用程序组件进行的任何更改都不会影响其子组件渲染 dom。更新值我只是使用 changeDetectorRef detectChanges 所以数据得到改变 通过这样做实际上在这个树渲染中发生了什么以及性能如何增加

标签: angularangular2-changedetectionangular-changedetection

解决方案


推荐阅读