changedetection 变更检查

2019-05-27  本文已影响0人  一只重拾梦想的小水

常见的迷之报错:Expression has changed after checked

参见这篇  https://www.jianshu.com/p/25f79e6245d9

ChangeDetectorRef: (组件a中引用)

返回一个a及以下的viewRef,即视图引用

1、状态(ChangeDetectorStatus)

* FirstCheck* ChecksEnabled* Errored* Destroyed

OnPush:

ChangeDetectionStrategy 设置成 OnPush: 在第一轮变更检测运行后禁止检测, 当父组件绑定属性改变时启用, 在变更检测执行后再禁用。

You probably know that Angular tracks binding inputs by object reference. It means that if an object reference hasn’t changed the binding change is not detected and change detection is not executed for a component that uses OnPush strategy. 

如果,binding的object reference引用不变,即使某一个属性变了,也不会触发

2、方法

detechChanges: 自顶向下,执行一轮变更检查,a, aa, ab, ...

detach/reattach: 禁用/启用,对a的变更检查。(如果a的父组件上没有触发检查,那么 a的检查状态的启用,就没有意义)

markForCheck: 自下向上,启用自己及所有父组件(直到根组件)的变更检查,一次????(ChangeDetectorStatus.CheckOnce)

checkNoChanges: 返回“是否没有更新”的bool值

todo:

markForCheck vs detechChanges:

各自适合的应用场景

生命周期:(待完善)

引用:

https://www.jianshu.com/p/0efd6148926d

原文:https://blog.angularindepth.com/everything-you-need-to-know-about-change-detection-in-angular-8006c51d206f

https://blog.angularindepth.com/if-you-think-ngdocheck-means-your-component-is-being-checked-read-this-article-36ce63a3f3e5

上一篇 下一篇

猜你喜欢

热点阅读