路线变化的角度变化检测

Imr*_*tif 2 javascript dom routes angular2-changedetection angular

我已经阅读了一些关于Change DetectionAngular 的好文章,并且对它是什么以及它是如何工作的有了一定的了解。但是到目前为止我读过的每篇文章都只关注组件上Change Detection何时发生某些事件或某些input属性更改时等。我还没有找到任何文章关注route更改时会发生什么?Change Detection在这种情况下如何工作?此外,Angular 是一次性将所有 HTML/DOM 更新推送到浏览器,还是在发现任何 DOM 更新后立即不断向浏览器提供信息?

Max*_*kyi 6

路由不会触发更改检测。它针对更改检测所做的唯一一件事是标记检查正在激活的路由器插座组件:

@Directive({selector: 'router-outlet', exportAs: 'outlet'})
export class RouterOutlet implements OnDestroy, OnInit {
    ...
    activateWith(activatedRoute: ActivatedRoute, resolver: ComponentFactoryResolver|null) {
        ...
        // Calling `markForCheck` to make sure we will run the change detection when the
        // `RouterOutlet` is inside a `ChangeDetectionStrategy.OnPush` component.
        this.changeDetector.markForCheck();
        this.activateEvents.emit(this.activated.instance);
    }
Run Code Online (Sandbox Code Playgroud)

路由器由一些事件 UI 或其他(setTimeout、XHR)等导航,这些 UI 被 NgZone 拦截,一旦代码完成执行变更检测过程就会启动。

此外,Angular 是一次性将所有 HTML/DOM 更新推送到浏览器,还是在发现任何 DOM 更新后立即不断向浏览器提供信息?

它在遍历每个组件时逐个元素更新 DOM。有关更改检测的最全面说明,请阅读以下所有文章: