您可以在 Angular 2 组件中同时使用 OnChanges 和 DoCheck 吗?

Can you use both OnChanges and DoCheck in an Angular 2 component?

我有一个场景,我希望在我的组件中同时使用 ngOnChangesngDoCheck,但是我记得很久以前在 angular 文档中一次只能使用其中一个。

虽然我似乎再也找不到此信息,但我认为它在之前 this 部分的某处说过。

同时使用这两种方法是否安全,或者我是否需要在 DoCheck 中实现我自己的 ngOnChanges 版本以避免造成巨大的 "no no"?

ngDoCheck 的 API 文档说

ngDoCheck gets called to check the changes in the directives in addition to the default algorithm.

开发指南 LifeCycle Hooks 文档说

We also see that the ngOnChanges method is called in contradiction of the incorrect API documentation.

但是 API 文档现在是正确的。 used to state 如果您实现了 ngDoCheck(),则不会调用默认算法——即不会调用 ngOnChanges()。 (因此,开发指南现在不正确地指出 API 文档不正确)。

两者都实现是非常好的。