fixture.detectChange是如何触发Component view的ngOnInit钩子的

117 阅读1分钟

在Angular单元测试it实现里设置断点,从运行时的上下文,能看出jasmine单元测试,运行在特殊的zone上:

一旦我执行上图116行的fixture.detectChanges():

最后就会转而调用我自己Component里的ngOnInit钩子:

Zone.prototype.run:

这个setNative应该就是window.setTimeout patch之后的版本:

这里还能看到XMLHttpRequest.send也被patch了:

patchMethod的实现:

ZoneDelegate.invoke会调用tickRootContext,里面会调用renderComponentOrTemplate:

detectChange最后会refreshView:

从而执行view的Component的各种hook:

更多Jerry的原创文章,尽在:“汪子熙”: