paint-brush
Hooking into the Angular bootstrap processby@maxim.koretskyi
1,702 reads
1,702 reads

Hooking into the Angular bootstrap process

by Maxim Koretskyi
Maxim Koretskyi HackerNoon profile picture

Maxim Koretskyi

@maxim.koretskyi

April 24th, 2017
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Today while exploring Angular bootstrap process I’ve come across an interesting <a href="https://github.com/angular/angular/blob/master/packages/core/src/application_ref.ts#L529">piece of&nbsp;code</a>:

Company Mentioned

Mention Thumbnail
Initialized
featured image - Hooking into the Angular bootstrap process
Maxim Koretskyi HackerNoon profile picture
Maxim Koretskyi

Maxim Koretskyi

@maxim.koretskyi

Today while exploring Angular bootstrap process I’ve come across an interesting piece of code:

private _loadComponent(componentRef: ComponentRef<any>): void {
  this.attachView(componentRef.hostView);
  this.tick();
  this._rootComponents.push(componentRef);
  // Get the listeners lazily to prevent DI cycles.
  const listeners =
      this._injector.get(APP_BOOTSTRAP_LISTENER, []).concat(this._bootstrapListeners);
  listeners.forEach((listener) => listener(componentRef));
}

This is the function that Angular calls when instantiating the application. Besides giving insights into how a component is added into the application, it also suggests that for each bootstrapped component Angular calls listeners registered under APP_BOOTSTRAP_LISTENER token and passes bootstrapped component to them.

It means that we can use such hooks to subscribe to the application bootstrap process and perform our initialization logic. For example, here is how Router hooks into to the process and executes some initialization.

Since Angular passes initialized component into the callback, we can get a hold of root ComponentRef of the application like this:

import {APP_BOOTSTRAP_LISTENER, ...} from '@angular/core';

@NgModule({
  imports: [BrowserModule, ReactiveFormsModule, TasksModule],
  declarations: [AppComponent, BComponent, AComponent, SComponent, LiteralsComponent],
  providers: [{
    provide: APP_BOOTSTRAP_LISTENER, multi: true, useFactory: () => {
      return (component: ComponentRef<any>) => {
        console.log(component.instance.title);
      }
    }
  }],
  bootstrap: [AppComponent]
})
export class AppModule {
}

After running into such functionality in the sources I checked the documentation and it’s described as experimental and the following description is provided:

All callbacks provided via this token will be called for every component that is bootstrapped. Signature of the callback:

(componentRef: ComponentRef) => void

Since there are no examples I thought I’d describe the functionality and show how it can be used.

P.S.

This article is short but I hope it gave you some useful insights. If so, I’d appreciate if you recommended it. I’m working on the few in-depth articles regarding bootstrap process, view composition and injectors hierarchy. If you’d like to get notified when they are finished — do follow me. Thanks!

L O A D I N G
. . . comments & more!

About Author

Maxim Koretskyi HackerNoon profile picture
Maxim Koretskyi@maxim.koretskyi

TOPICS

THIS ARTICLE WAS FEATURED IN...

Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Coderoad
Voidcc

Mentioned in this story

companies
X REMOVE AD