- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个使用 ngFor
显示子组件列表的父组件。我注意到随着 child 数量的增加,性能变得非常糟糕,所以我将两者都更改为 OnPush
更改检测策略。
这很有帮助,但仍然有一些情况会变慢,我可以看出这是因为对每个 child 不必要地执行了变化检测。
一个例子是当子组件内部有一个点击事件时——即使没有输入被改变并且它只是触发了一个动画,出于某种原因,变化检测正在为父组件执行,结果是为每个子组件同样(尽管 ngFor
背后的模型根本没有改变,它是一个 OnPush
策略......)。我本以为这种“隔离”事件应该只触发那个特定子组件的变化检测而不是向上传播(我实际上已经尝试过 event.stopPropagation()
和 event.preventDefault ()
没有成功)。
所以我想知道两件事:
1) 是否有任何方法可以更好地控制实际运行的事件更改检测以及它是否也触发父组件更改检测?
2) 在每个子组件(来自 ng2translate)中大量使用“翻译”管道会减慢应用程序/更改检测的速度吗?
下面的示例 plunkr 显示问题所在。基本上,如果我点击 ngFor 列表中的任何项目,它会为每个 child 启动变化检测,而不仅仅是受影响的 child ,我想知道是否有任何方法可以抑制它。
最佳答案
1) 您可以使用ChangeDetectorRef.detach()
https://angular.io/docs/js/latest/api/core/index/ChangeDetectorRef-class.html#!#detach-anchor
Detaches the change detector from the change detector tree.
The detached change detector will not be checked until it is reattached.
This can also be used in combination with ChangeDetectorRef to implement local change detection checks.
2) 管道(如果它们是纯管道,这是默认设置)仅在管道值或参数更改时调用,因此没有性能劣势。
关于performance - Angular2 OnPush 变化检测和 ngFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42067346/
我有两个组件,都设置为 OnPush。调用 getAccount() 后,父组件将 accountLoading 设置为 true,然后在调用完成后将 accountLoading 设置为 false
我正在尝试衡量变化检测策略的性能差异。 我已经添加了 Angular 分析器,并使用 Default 检查,然后将 onPush 添加到我们的大多数组件,这些是结果:(开发模式) 默认:{ msPer
我不明白为什么子组件的更改检测会在这种情况下运行: import { Component, ChangeDetectionStrategy } from '@angular/core' @Compon
我不明白为什么子组件的更改检测会在这种情况下运行: import { Component, ChangeDetectionStrategy } from '@angular/core' @Compon
最好的使用方法是什么OnPush更改检测可以使更改灵活并且在测试中也非常简单?通过各种教程,我发现可以使用 OnPush : 首先是在 View 中直接使用服务中的可观察对象,我不确定该方法是否有效。
可能是我误解了 Angular changeDetection 策略。 这里是描述: 我有 2 个同级组件(S1 和 S2) 将这两个组件都视为显示一些图形数据的小部件 加载时,我想一开始就在两个
我正在编写一个 Angular 2 应用程序,出于性能原因,我尝试在所有地方使用 ChangeDetectionStrategy.OnPush。我有一个复杂的组件需要 OnPush 才能顺利工作,其中
我有一个看起来像这样的子组件: @Component({ selector: 'app-child', changeDetection: ChangeDetectionStrategy.OnP
我正在尝试熟悉 Angular 2 的 ChangeDetectionStrategy.OnPush 性能提升(如 here 所述)。但我这里有古玩盒。 我有父 AppComponent: @Comp
我正在构建一个包含许多组件的 Angular 4 应用程序,其中 ChangeDetectionStrategy 是 OnPush。同时the Angular Documentation on the
我是否应该始终在我的组件中使用 ChangeDetectionStrategy.OnPush? 我总是听说 OnPush 非常棒,它解决了很多问题,加快了 Angular 应用程序的速度,甚至摆脱了
我有一个数据表组件 ( angular2-data-table) 项目,我们将项目从 Angular 的传统变化检测更改为 OnPush 以优化渲染速度。 实现新的变更检测策略后,当数据对象发生突变(
我有一个使用服务提交一些数据的组件。 该组件使用 ChangeDetectionStrategy.OnPush ,但发生了一些奇怪的事情: 当请求 成功 : next回调被调用,formStatus更
正如标题所说:我正在处理一个非常大的项目,而且我使用过的组件很少 ChangeDetectionStrategy.OnPush以免表现不佳。我想知道,将策略的每个组件都放入“好”,以防万一,使用 Ch
我在理解 Angular OnPush 检测策略时遇到问题。当我执行一些异步操作时(例如从 TS 打开覆盖,这样就不会导致使用 OnPush 策略检测组件上的更改),然后我单击任意位置,或调用其他检测
我想在使用 OnPush 变化检测策略上传之前预览多张图片。 我试试这个 https://stackblitz.com/edit/angular-mnltiv 当我添加 OnPush 时它停止工作,我
我有一个使用 ngFor 显示子组件列表的父组件。我注意到随着 child 数量的增加,性能变得非常糟糕,所以我将两者都更改为 OnPush 更改检测策略。 这很有帮助,但仍然有一些情况会变慢,我可以
https://stackblitz.com/edit/angular-xpamld 问题:有人能帮我理解为什么我的原型(prototype)的 changeDetection: ChangeDete
当我们使用默认策略时,这些家伙可以触发变更检测(当然除了输入参数): 用户事件 计时器 ajax 响应 但是。当您切换到 OnPush 策略 时,它仅由事件 触发并且不适用于计时器和 http。 所以
@Component({ selector: "parent", template: ``, changeDetection: ChangeDetectionStrategy.
我是一名优秀的程序员,十分优秀!