我需要查看我的应用程序中运行了多少变化检测周期(以及谁发起了它)——它有很多组件。
目前,我知道,在每个组件中,我可以使用 ngDoCheck
方法:
ngDoCheck()
Detect and act upon changes that Angular can't or won't detect on its own. Called during every change detection run, immediately after ngOnChanges() and ngOnInit().
这不是一个糟糕的解决方案。
问题:
但是 - 有没有办法全局做到这一点?我的意思是,我真的不想修改每个组件并添加这段代码。
有没有一种方法可以让“监听器和记录器”处于全局范围内,以便我知道我的应用程序的哪一部分会导致许多更改检测周期?
我的目标(如果可能的话):
(console.log) -- from component A
(console.log) -- from component A
(console.log) -- from component A
(console.log) -- from component B
(console.log) -- from component C
(console.log) -- from component C
最佳答案
AfterViewChecked
应该可以帮助您:
Respond after Angular checks the component's views and child views / the view that a directive is in. Called after the ngAfterViewInit and every subsequent ngAfterContentChecked().
如果您在 AppComponent
中实现 AfterViewChecked
,它应该在每次任何子组件发生任何变化时触发:
ngAfterViewChecked() {
console.trace();
}
关于javascript - 记录 Angular 中的变更检测数量(全局),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50963402/