简体   繁体   English

Angular NgZone.runOutsideAngular 和 OnPush 变化检测策略

[英]Angular NgZone.runOutsideAngular and OnPush change detection strategy

I'm trying to optimize the performance of code in angular application.我正在尝试优化 angular 应用程序中代码的性能。

I know that I can runOutsideAngular some code in order not to cause change detection on each code run or event.我知道我可以runOutsideAngular一些代码,以免在每个代码运行或事件上引起更改检测。

However, do I need to use runOutsideAngular when I utilize OnPush change detection strategy?但是,在使用OnPush更改检测策略时是否需要使用runOutsideAngular (for listeners etc) (对于听众等)

I think I have an answer now.我想我现在有了答案。

In my opinion it depends on what an app looks like.在我看来,这取决于应用程序的外观。

  • If it has lots of unoptimized components and the an async operation is going to fire lots of times or it's an event listener then it has to be within runOutsideAngular如果它有很多未优化的组件并且异步操作触发很多次或者它是一个事件侦听器,那么它必须在runOutsideAngular

  • However, if an application is mostly built with components with OnPush strategy or simply AppComponent uses OnPush (as whole tree is going to be using OnPush strategy defining it on a top level component defines this strategy for the whole application irreversibly) then you can omit using runOutsideAngular as triggers for change detection won't affect anything但是,如果应用程序主要使用具有OnPush策略的组件构建,或者只是AppComponent使用OnPush (因为整个树将使用OnPush策略在顶级组件上定义它,为整个应用程序不可逆转地定义此策略),那么您可以省略使用runOutsideAngular作为更改检测的触发器不会影响任何事情

Also I think that there is an option for fully OnPush optimized app to disable all events patches from Zone thus there would be no need for runOutsideAngular as all async events have no point of triggering NgZone.此外,我认为完全OnPush优化的应用程序有一个选项可以禁用来自 Zone 的所有事件补丁,因此不需要runOutsideAngular因为所有异步事件都没有触发 NgZone 的意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM