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:
		
							parent
							
								
									fed6a7ce7d
								
							
						
					
					
						commit
						cf5f86386f
					
				| @ -27,6 +27,59 @@ import {noop} from './util/noop'; | |||||||
|  * |  * | ||||||
|  * @see `ApplicationInitStatus` |  * @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 |  * @publicApi | ||||||
|  */ |  */ | ||||||
| export const APP_INITIALIZER = | export const APP_INITIALIZER = | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user