Angular框架解讀--依賴注入的引導(dǎo)過程
作為“為大型前端項目”而設(shè)計的前端框架,Angular 其實有許多值得參考和學(xué)習(xí)的設(shè)計,本系列主要用于研究這些設(shè)計和功能的實現(xiàn)原理。本文主要圍繞 Angular 中的最大特點——依賴注入,介紹 Angular 依賴注入在體系在應(yīng)用引導(dǎo)過程中的的設(shè)計和實現(xiàn)。
多級依賴注入中,介紹了模塊注入器和元素注入器兩種層次結(jié)構(gòu)的注入器。那么,Angular 在引導(dǎo)過程中,又是如何初始化根模塊和入口組件的呢?
Angular 的引導(dǎo)過程
前面我們說到,Angular 應(yīng)用在瀏覽器中引導(dǎo)時,會創(chuàng)建瀏覽器平臺,并引導(dǎo)根模塊:
- platformBrowserDynamic().bootstrapModule(AppModule);
引導(dǎo)根模塊
根模塊 AppModule
在 Angular 中,每個應(yīng)用有至少一個 Angular 模塊,根模塊就是你用來引導(dǎo)此應(yīng)用的模塊,它通常命名為 AppModule。
當你使用 Angular CLI 命令 ng new 生成一個應(yīng)用時,其默認的 AppModule 是這樣的:
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { AppComponent } from './app.component';
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
引導(dǎo)根模塊的過程
我們來看看平臺層引導(dǎo)根模塊的過程中都做了些什么:
- @Injectable()
- export class PlatformRef {
- ...
- bootstrapModuleFactory<M>(moduleFactory: NgModuleFactory<M>, options?: BootstrapOptions):
- Promise<NgModuleRef<M>> {
- // 由于實例化模塊時,會需要創(chuàng)建一些提供者,所以這里需要在實例化模塊之前創(chuàng)建 NgZone
- // 因此,這里創(chuàng)建了一個僅包含新 NgZone 的微型父注入器,并將其作為父傳遞給 NgModuleFactory
- const ngZoneOption = options ? options.ngZone : undefined;
- const ngZoneEventCoalescing = (options && options.ngZoneEventCoalescing) || false;
- const ngZoneRunCoalescing = (options && options.ngZoneRunCoalescing) || false;
- const ngZone = getNgZone(ngZoneOption, {ngZoneEventCoalescing, ngZoneRunCoalescing});
- const providers: StaticProvider[] = [{provide: NgZone, useValue: ngZone}];
- // ApplicationRef 將在 Angular zone 之外創(chuàng)建
- return ngZone.run(() => {
- // 在 ngZone.run 中創(chuàng)建 ngZoneInjector,以便在 Angular zone 中創(chuàng)建所有實例化的服務(wù)
- const ngZoneInjector = Injector.create(
- {providers: providers, parent: this.injector, name: moduleFactory.moduleType.name});
- const moduleRef = <InternalNgModuleRef<M>>moduleFactory.create(ngZoneInjector);
- const exceptionHandler: ErrorHandler|null = moduleRef.injector.get(ErrorHandler, null);
- if (!exceptionHandler) {
- throw new Error('No ErrorHandler. Is platform module (BrowserModule) included?');
- }
- ...
- return _callAndReportToErrorHandler(exceptionHandler, ngZone!, () => {
- const initStatus: ApplicationInitStatus = moduleRef.injector.get(ApplicationInitStatus);
- initStatus.runInitializers();
- return initStatus.donePromise.then(() => {
- ...
- // 引導(dǎo)模塊
- this._moduleDoBootstrap(moduleRef);
- return moduleRef;
- });
- });
- });
- }
- bootstrapModule<M>(
- moduleType: Type<M>,
- compilerOptions: (CompilerOptions&BootstrapOptions)|
- Array<CompilerOptions&BootstrapOptions> = []): Promise<NgModuleRef<M>> {
- const options = optionsReducer({}, compilerOptions);
- // 編譯并創(chuàng)建 @NgModule 的實例
- return compileNgModuleFactory(this.injector, options, moduleType)
- .then(moduleFactory => this.bootstrapModuleFactory(moduleFactory, options));
- }
- private _moduleDoBootstrap(moduleRef: InternalNgModuleRef<any>): void {
- const appRef = moduleRef.injector.get(ApplicationRef) as ApplicationRef;
- // 引導(dǎo)應(yīng)用程序
- if (moduleRef._bootstrapComponents.length > 0) {
- // 在應(yīng)用程序的根級別引導(dǎo)新組件
- moduleRef._bootstrapComponents.forEach(f => appRef.bootstrap(f));
- } else if (moduleRef.instance.ngDoBootstrap) {
- moduleRef.instance.ngDoBootstrap(appRef);
- } else {
- ...
- }
- this._modules.push(moduleRef);
- }
- }
根模塊引導(dǎo)時,除了編譯并創(chuàng)建 AppModule 的實例,還會創(chuàng)建 NgZone,關(guān)于 NgZone 的請參考。在編譯和創(chuàng)建 AppModule 的過程中,便會創(chuàng)建 ApplicationRef
,即 Angular 應(yīng)用程序。
引導(dǎo) Angular 應(yīng)用程序
前面在引導(dǎo)根模塊過程中,創(chuàng)建了 Angular 應(yīng)用程序之后,便會在應(yīng)用程序的根級別引導(dǎo)新組件:
- // 在應(yīng)用程序的根級別引導(dǎo)新組件
- moduleRef._bootstrapComponents.forEach(f => appRef.bootstrap(f));
我們來看看這個過程會發(fā)生什么。
應(yīng)用程序 ApplicationRef
一個 Angular 應(yīng)用程序,提供了以下的能力:
- @Injectable()
- export class ApplicationRef {
- // 獲取已注冊到該應(yīng)用程序的組件類型的列表
- public readonly componentTypes: Type<any>[] = [];
- // 獲取已注冊到該應(yīng)用程序的組件的列表
- public readonly components: ComponentRef<any>[] = [];
- // 返回一個 Observable,指示應(yīng)用程序何時穩(wěn)定或不穩(wěn)定
- // 如果在應(yīng)用程序引導(dǎo)時,引導(dǎo)任何種類的周期性異步任務(wù),則該應(yīng)用程序?qū)⒂肋h不會穩(wěn)定(例如輪詢過程)
- public readonly isStable!: Observable<boolean>;
- constructor(
- private _zone: NgZone, private _injector: Injector, private _exceptionHandler: ErrorHandler,
- private _componentFactoryResolver: ComponentFactoryResolver,
- private _initStatus: ApplicationInitStatus) {
- // 創(chuàng)建時,主要進行兩件事:
- // 1. 宏任務(wù)結(jié)束后,檢測視圖是否需要更新。
- // 2. 在 Angular Zone 之外創(chuàng)建對 onStable 的預(yù)訂,以便在 Angular Zone 之外運行回調(diào)。
- }
- // 在應(yīng)用程序的根級別引導(dǎo)新組件
- bootstrap<C>(componentOrFactory: ComponentFactory<C>|Type<C>, rootSelectorOrNode?: string|any):
- ComponentRef<C> {}
- // 調(diào)用此方法以顯式處理更改檢測及其副作用
- tick(): void {}
- // 關(guān)聯(lián)視圖,以便對其進行臟檢查,視圖銷毀后將自動分離
- attachView(viewRef: ViewRef): void {}
- // 再次從臟檢查中分離視圖
- detachView(viewRef: ViewRef): void {}
- }
那么,我們來看看 bootstrap()
過程中,Angular 都做了些什么。
在應(yīng)用程序的根級別引導(dǎo)根組件
將新的根組件引導(dǎo)到應(yīng)用程序中時,Angular 將指定的應(yīng)用程序組件安裝到由 componentType
的選擇器標識的 DOM 元素上,并引導(dǎo)自動更改檢測以完成組件的初始化。
- @Injectable()
- export class ApplicationRef {
- bootstrap<C>(componentOrFactory: ComponentFactory<C>|Type<C>, rootSelectorOrNode?: string|any):
- ComponentRef<C> {
- ...
- // 如果未與其他模塊綁定,則創(chuàng)建與當前模塊關(guān)聯(lián)的工廠
- const ngModule =
- isBoundToModule(componentFactory) ? undefined : this._injector.get(NgModuleRef);
- const selectorOrNode = rootSelectorOrNode || componentFactory.selector;
- // 創(chuàng)建組件
- const compRef = componentFactory.create(Injector.NULL, [], selectorOrNode, ngModule);
- const nativeElement = compRef.location.nativeElement;
- // 創(chuàng)建可測試服務(wù)掛鉤
- const testability = compRef.injector.get(Testability, null);
- const testabilityRegistry = testability && compRef.injector.get(TestabilityRegistry);
- if (testability && testabilityRegistry) {
- testabilityRegistry.registerApplication(nativeElement, testability);
- }
- // 組件銷毀時,銷毀關(guān)聯(lián)視圖以及相關(guān)的服務(wù)
- compRef.onDestroy(() => {
- this.detachView(compRef.hostView);
- remove(this.components, compRef);
- if (testabilityRegistry) {
- testabilityRegistry.unregisterApplication(nativeElement);
- }
- });
- // 加載組件,包括關(guān)聯(lián)視圖、監(jiān)聽變更等
- this._loadComponent(compRef);
- ...
- return compRef;
- }
- }
在創(chuàng)建根組件的過程中,會關(guān)聯(lián) DOM 元素視圖、添加對狀態(tài)變更的檢測機制。
根組件是一個入口組件,Angular CLI 創(chuàng)建的默認應(yīng)用只有一個組件 AppComponent
,Angular 會在引導(dǎo)過程中把它加載到 DOM 中。
在根組件的創(chuàng)建過程中,通常會根據(jù)根組件中引用到的其他組件,觸發(fā)一系列組件的創(chuàng)建并形成組件樹。大多數(shù)應(yīng)用只有一個組件樹,并且只從一個根組件開始引導(dǎo)。
創(chuàng)建組件過程
Angular 中創(chuàng)建組件的過程如下:
- 當 Angular 創(chuàng)建組件類的新實例時,它會通過查看該組件類的構(gòu)造函數(shù),來決定該組件依賴哪些服務(wù)或其它依賴項。
- 當 Angular 發(fā)現(xiàn)某個組件依賴某個服務(wù)時,它會首先檢查是否該注入器中已經(jīng)有了那個服務(wù)的任何現(xiàn)有實例。如果所請求的服務(wù)尚不存在,注入器就會使用以前注冊的服務(wù)提供者來制作一個,并把它加入注入器中,然后把該服務(wù)返回給 Angular。
- 當所有請求的服務(wù)已解析并返回時,Angular 可以用這些服務(wù)實例為參數(shù),調(diào)用該組件的構(gòu)造函數(shù)。
Angular 會在執(zhí)行應(yīng)用時創(chuàng)建注入器,第一個注入器是根注入器,創(chuàng)建于引導(dǎo)過程中。借助注入器繼承機制,可以把全應(yīng)用級的服務(wù)注入到這些組件中。
到這里,Angular 分別完成了根模塊、根組件和組件樹的引導(dǎo)過程,通過編譯器則可以將組件和視圖渲染到頁面上。
總結(jié)
在應(yīng)用程序的引導(dǎo)過程中,Angular 采取了以下步驟來加載我們的第一個視圖:
- index.html
- Main.ts
本文我們重點從根模塊的引導(dǎo)過程開始,介紹了引導(dǎo) Angular 應(yīng)用程序、引導(dǎo)根組件、組件的創(chuàng)建等過程。至于組件樹的創(chuàng)建和渲染,則可以參考 編譯器 相關(guān)的內(nèi)容。