fix(provider): fix a circular dependency & remove common providers
This commit is contained in:
		
							parent
							
								
									97cf0e40d5
								
							
						
					
					
						commit
						25c6a3715d
					
				| @ -1,62 +0,0 @@ | |||||||
| import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common'; |  | ||||||
| import {APP_INITIALIZER, ApplicationRef, ComponentResolver, Injector} from '@angular/core'; |  | ||||||
| 
 |  | ||||||
| import {RouterConfig} from './config'; |  | ||||||
| import {Router} from './router'; |  | ||||||
| import {RouterOutletMap} from './router_outlet_map'; |  | ||||||
| import {ActivatedRoute} from './router_state'; |  | ||||||
| import {DefaultUrlSerializer, UrlSerializer} from './url_serializer'; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * A list of {@link Provider}s. To use the router, you must add this to your application. |  | ||||||
|  * |  | ||||||
|  * ### Example |  | ||||||
|  * |  | ||||||
|  * ``` |  | ||||||
|  * @Component({directives: [ROUTER_DIRECTIVES]}) |  | ||||||
|  * class AppCmp { |  | ||||||
|  *   // ...
 |  | ||||||
|  * } |  | ||||||
|  * |  | ||||||
|  * const router = [ |  | ||||||
|  *   {path: '/home', component: Home} |  | ||||||
|  * ]; |  | ||||||
|  * |  | ||||||
|  * bootstrap(AppCmp, [provideRouter(router)]); |  | ||||||
|  * ``` |  | ||||||
|  */ |  | ||||||
| export function provideRouter(config: RouterConfig): any[] { |  | ||||||
|   return [ |  | ||||||
|     Location, |  | ||||||
|     {provide: LocationStrategy, useClass: PathLocationStrategy}, |  | ||||||
|     {provide: UrlSerializer, useClass: DefaultUrlSerializer}, |  | ||||||
| 
 |  | ||||||
|     { |  | ||||||
|       provide: Router, |  | ||||||
|       useFactory: (ref, resolver, urlSerializer, outletMap, location, injector) => { |  | ||||||
|         if (ref.componentTypes.length == 0) { |  | ||||||
|           throw new Error('Bootstrap at least one component before injecting Router.'); |  | ||||||
|         } |  | ||||||
|         const componentType = ref.componentTypes[0]; |  | ||||||
|         const r = new Router( |  | ||||||
|             componentType, resolver, urlSerializer, outletMap, location, injector, config); |  | ||||||
|         ref.registerDisposeListener(() => r.dispose()); |  | ||||||
|         return r; |  | ||||||
|       }, |  | ||||||
|       deps: |  | ||||||
|           [ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector] |  | ||||||
|     }, |  | ||||||
| 
 |  | ||||||
|     RouterOutletMap, |  | ||||||
|     {provide: ActivatedRoute, useFactory: (r) => r.routerState.root, deps: [Router]}, |  | ||||||
| 
 |  | ||||||
|     // Trigger initial navigation
 |  | ||||||
|     { |  | ||||||
|       provide: APP_INITIALIZER, |  | ||||||
|       multi: true, |  | ||||||
|       useFactory: (router: Router) => router.initialNavigation(), |  | ||||||
|       deps: [Router] |  | ||||||
|     }, |  | ||||||
|   ]; |  | ||||||
| } |  | ||||||
| @ -1,12 +1,15 @@ | |||||||
| import {PlatformLocation} from '@angular/common'; | import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common'; | ||||||
| import {BrowserPlatformLocation} from '@angular/platform-browser'; | import {APP_INITIALIZER, ApplicationRef, ComponentResolver, Injector} from '@angular/core'; | ||||||
| 
 | 
 | ||||||
| import * as common from './common_router_providers'; |  | ||||||
| import {RouterConfig} from './config'; | import {RouterConfig} from './config'; | ||||||
|  | import {Router} from './router'; | ||||||
|  | import {RouterOutletMap} from './router_outlet_map'; | ||||||
|  | import {ActivatedRoute} from './router_state'; | ||||||
|  | import {DefaultUrlSerializer, UrlSerializer} from './url_serializer'; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * A list of {@link Provider}s. To use the router, you must add this to your application. |  * A list of providers. To use the router, you must add this to your application. | ||||||
|  * |  * | ||||||
|  * ### Example |  * ### Example | ||||||
|  * |  * | ||||||
| @ -16,15 +19,50 @@ import {RouterConfig} from './config'; | |||||||
|  *   // ...
 |  *   // ...
 | ||||||
|  * } |  * } | ||||||
|  * |  * | ||||||
|  * const router = [ |  * const routes = [ | ||||||
|  *   {path: '/home', component: Home} |  *   {path: '/home', component: Home} | ||||||
|  * ]; |  * ]; | ||||||
|  * |  * | ||||||
|  * bootstrap(AppCmp, [provideRouter(router)]); |  * bootstrap(AppCmp, [provideRouter(routes)]); | ||||||
|  * ``` |  * ``` | ||||||
|  */ |  */ | ||||||
| export function provideRouter(config: RouterConfig): any[] { | export function provideRouter(config: RouterConfig): any[] { | ||||||
|   return [ |   return [ | ||||||
|     {provide: PlatformLocation, useClass: BrowserPlatformLocation}, ...common.provideRouter(config) |     Location, | ||||||
|  |     {provide: LocationStrategy, useClass: PathLocationStrategy}, | ||||||
|  |     {provide: UrlSerializer, useClass: DefaultUrlSerializer}, | ||||||
|  | 
 | ||||||
|  |     { | ||||||
|  |       provide: Router, | ||||||
|  |       useFactory: (ref, resolver, urlSerializer, outletMap, location, injector) => { | ||||||
|  |         if (ref.componentTypes.length == 0) { | ||||||
|  |           throw new Error('Bootstrap at least one component before injecting Router.'); | ||||||
|  |         } | ||||||
|  |         const componentType = ref.componentTypes[0]; | ||||||
|  |         const r = new Router( | ||||||
|  |             componentType, resolver, urlSerializer, outletMap, location, injector, config); | ||||||
|  |         ref.registerDisposeListener(() => r.dispose()); | ||||||
|  |         return r; | ||||||
|  |       }, | ||||||
|  |       deps: | ||||||
|  |           [ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector] | ||||||
|  |     }, | ||||||
|  | 
 | ||||||
|  |     RouterOutletMap, | ||||||
|  |     {provide: ActivatedRoute, useFactory: (router) => router.routerState.root, deps: [Router]}, | ||||||
|  | 
 | ||||||
|  |     // Trigger initial navigation
 | ||||||
|  |     { | ||||||
|  |       provide: APP_INITIALIZER, | ||||||
|  |       multi: true, | ||||||
|  |       useFactory: (injector) => { | ||||||
|  |         // https://github.com/angular/angular/issues/9101
 | ||||||
|  |         // Delay the router instantiation to avoid circular dependency (ApplicationRef ->
 | ||||||
|  |         // APP_INITIALIZER -> Router)
 | ||||||
|  |         setTimeout(_ => injector.get(Router).initialNavigation(), 0); | ||||||
|  |         return _ => null; | ||||||
|  |       }, | ||||||
|  |       deps: [Injector] | ||||||
|  |     }, | ||||||
|   ]; |   ]; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user