provideAppInitializer
The provided function is injected at application startup and executed during app initialization. If the function returns a Promise or an Observable, initialization does not complete until the Promise is resolved or the Observable is completed.
API
function provideAppInitializer( initializerFn: () => void | Observable<unknown> | Promise<unknown>): EnvironmentProviders;
provideAppInitializer
The provided function is injected at application startup and executed during app initialization. If the function returns a Promise or an Observable, initialization does not complete until the Promise is resolved or the Observable is completed.
You can, for example, create a function that loads language data
or an external configuration, and provide that function using provideAppInitializer()
.
The function is executed during the application bootstrap process,
and the needed data is available on startup.
Note that the provided initializer is run in the injection context.
Previously, this was achieved using the APP_INITIALIZER
token which is now deprecated.
() => void | Observable<unknown> | Promise<unknown>
EnvironmentProviders
Description
The provided function is injected at application startup and executed during app initialization. If the function returns a Promise or an Observable, initialization does not complete until the Promise is resolved or the Observable is completed.
You can, for example, create a function that loads language data
or an external configuration, and provide that function using provideAppInitializer()
.
The function is executed during the application bootstrap process,
and the needed data is available on startup.
Note that the provided initializer is run in the injection context.
Previously, this was achieved using the APP_INITIALIZER
token which is now deprecated.
Usage Notes
The following example illustrates how to configure an initialization function using
provideAppInitializer()
bootstrapApplication(App, { providers: [ provideAppInitializer(() => { const http = inject(HttpClient); return firstValueFrom( http .get("https://someUrl.com/api/user") .pipe(tap(user => { ... })) ); }), provideHttpClient(), ],});