为什么即使OnPush策略为ON,事件也会触发ChangeDetection? [英] Why event triggers ChangeDetection even if OnPush strategy is ON?

查看:91
本文介绍了为什么即使OnPush策略为ON,事件也会触发ChangeDetection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用默认策略时,这些家伙可能会触发变更检测(当然,输入参数除外):

When we use Default Strategy this guys could trigger Change Detection(of course except input params):

  • 用户事件
  • 计时器
  • ajax响应

但是.当您切换到 OnPush策略时,它仅由事件触发,不适用于计时器和http.

BUT. When you switch to OnPush Strategy it's trigger only by events and does not work for timers and http.

所以问题是为什么它对时间和https不起作用,或者为什么对事件不起作用.

So the questions is why it does not work for times and https or why it works for events.

推荐答案

OnPush是用这种方式定义的.

OnPush is defined this way.

它触发变更检测

  • 收到组件侦听的DOM事件
  • |async管道收到新事件时
  • 通过更改检测更新@Input()时.
  • 使用ChangeDetectorRef::markForCheck
  • 显式注册要检查的组件时,进行下一次更改检测时
  • when a DOM event the component listens to was received
  • when the |async pipe receives a new event
  • when an @Input() was updated by change detection.
  • when explicitly registering the component to be checked the next change detection turn using ChangeDetectorRef::markForCheck

ChangeDetectionStrategy.Default触发对Angulars区域中调用的每个异步回调的更改检测(每个DOM甚至在Angular应用程序中侦听的每个DOM,每个Observable事件或已完成的Promise,setTimeout等)

ChangeDetectionStrategy.Default triggers change detection for every async callback called within Angulars zone (every DOM even listened to within the Angular application, every Observable event or completed Promise, setTimeout, ...)

这篇关于为什么即使OnPush策略为ON,事件也会触发ChangeDetection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆