docs(core): add usage examples for APP_INITIALIZER token (#41095)

Add multiple usage examples for APP_INITIALIZER using Promise, Observable and multi providers

Closes #40730

PR Close #41095
This commit is contained in:
abarghoud 2021-03-05 23:03:16 +01:00 committed by Andrew Kushnir
parent fed6a7ce7d
commit cf5f86386f
1 changed files with 53 additions and 0 deletions

View File

@ -27,6 +27,59 @@ import {noop} from './util/noop';
*
* @see `ApplicationInitStatus`
*
* @usageNotes
*
* The following example illustrates how to configure a multi-provider using `APP_INITIALIZER` token
* and a function returning a promise.
*
* ```
* function initializeApp(): Promise<any> {
* return new Promise((resolve, reject) => {
* // Do some asynchronous stuff
* resolve();
* });
* }
*
* @NgModule({
* imports: [BrowserModule],
* declarations: [AppComponent],
* bootstrap: [AppComponent],
* providers: [{
* provide: APP_INITIALIZER,
* useFactory: () => initializeApp,
* multi: true
* }]
* })
* export class AppModule {}
* ```
*
* It's also possible to configure a multi-provider using `APP_INITIALIZER` token and a function
* returning an observable, see an example below. Note: the `HttpClient` in this example is used for
* demo purposes to illustrate how the factory function can work with other providers available
* through DI.
*
* ```
* function initializeApp(httpClient: HttpClient): Observable<any> {
* return httpClient.get("https://someUrl.com/api/user")
* .pipe(
* tap(user => { ... })
* )
* }
*
* @NgModule({
* imports: [BrowserModule, HttpClientModule],
* declarations: [AppComponent],
* bootstrap: [AppComponent],
* providers: [{
* provide: APP_INITIALIZER,
* useFactory: initializeApp,
* deps: [HttpClient],
* multi: true
* }]
* })
* export class AppModule {}
* ```
*
* @publicApi
*/
export const APP_INITIALIZER =