How To Use APP_INITIALIZER In Angular
We examine what APP INITIALIZER is in this article and how to use it in Angular applications. By building an example app, we'll demonstrate how to use it.
What is APP_INITIALIZER
An instance of InjectionToken is the APP INITIALIZER. It is an injection token that is already incorporated into Angular.
When the application loads, Angular will run the function given by this token. Angular will wait until the promise is resolved if the function returns a promise. Because of this, it will be the perfect location to implement some startup logic before the application is started.
Where to use APP_INITIALIZER
The APP INITIALIZER is launched when the application is initialized, as was already explained. Until all of the APP INITIALIZER's supplied functions are used, Angular suspends the initialization of the app. Before continuing with the initialization of the App, Angular waits for any promises returned by those initializers to resolve.
We now have the chance to run some of our application's custom logic by hooking into the initialization procedure. Runtime configuration data can be loaded. import critical information from the backend, etc.
Example
Create a new Angular Application
Create an app-init.service.ts file in the src/app folder.
import { Injectable } from '@angular/core'; @Injectable() export class AppInitService { constructor() { } Init() { return new Promise<void>((resolve, reject) => { console.log("AppInitService.init() called"); ////do your initialisation stuff here setTimeout(() => { console.log('AppInitService Finished'); resolve(); }, 6000); }); } }
There is only one method, Init, in this straightforward service. A Promise is returned by the method.
return new Promise<void>((resolve, reject) => {
import { BrowserModule } from '@angular/platform-browser'; import { NgModule, APP_INITIALIZER } from '@angular/core'; import { HttpClientModule } from '@angular/common/http'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { AboutUsComponent } from './about-us.component'; import { HomeComponent } from './home.component'; import { ContactUsComponent } from './contact-us.component'; import { AppInitService } from './app-init.service'; export function initializeApp1(appInitService: AppInitService) { return (): Promise<any> => { return appInitService.Init(); } } @NgModule({ declarations: [ AppComponent, AboutUsComponent,HomeComponent,ContactUsComponent ], imports: [ HttpClientModule, BrowserModule, AppRoutingModule, ], providers: [ AppInitService, { provide: APP_INITIALIZER,useFactory: initializeApp1, deps: [AppInitService], multi: true} ], bootstrap: [AppComponent] }) export class AppModule { }
First, import APP INITIALIZER from @angular/core.
import { NgModule, APP_INITIALIZER } from '@angular/core';
We must run the appInitService.Init(). We are unable to do it straight from the provider. A function that calls appInitService.Init() and returns a Promise must be written. In the initializeApp1 method, we accomplish that.
import { AppInitService } from './app-init.service'; export function initializeApp1(appInitService: AppInitService) { return (): Promise<any> => { return appInitService.Init(); } }
Finally,. To supply the initializeApp1 command, use the APP INITIALIZER token.
providers: [ AppInitService, { provide: APP_INITIALIZER,useFactory: initializeApp1, deps: [AppInitService], multi: true} ],
Multi Providers in APP_INITIALIZER
export function initializeApp2() { return (): Promise<any> => { return new Promise((resolve, reject) => { console.log(`initializeApp2 called`); setTimeout(() => { console.log(`initializeApp2 Finished`); resolve(); }, 2000); }); }; }
As seen below, register it next with the APP INITIALIZER token.
providers: [ AppInitService, { provide: APP_INITIALIZER,useFactory: initializeApp1, deps: [AppInitService], multi: true}, { provide: APP_INITIALIZER,useFactory: initializeApp2, multi: true} ],
Run the app. The following will be seen by you:
- InitializeApp1 and InitializeApp2 execute one after the other without pausing.
- Despite being called after initializeApp1, initializeApp2 completes execution first.
- In order for both functions to complete, Angular waits.