From d15a1d64e1d97aacc389b0970ff6bf2e2331d262 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Mon, 25 Jul 2016 16:10:10 -0700 Subject: [PATCH] fix(router): make router provides work with cli and offline compilation --- .../@angular/router/src/common_router_providers.ts | 7 +++++-- modules/@angular/router/src/router_module.ts | 6 +++--- .../router/testing/router_testing_module.ts | 13 +++++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/modules/@angular/router/src/common_router_providers.ts b/modules/@angular/router/src/common_router_providers.ts index 1c408170eb..4b5300107b 100644 --- a/modules/@angular/router/src/common_router_providers.ts +++ b/modules/@angular/router/src/common_router_providers.ts @@ -47,6 +47,10 @@ export function setupRouter( return r; } +export function rootRoute(router: Router): ActivatedRoute { + return router.routerState.root; +} + export function setupRouterInitializer(injector: Injector) { // https://github.com/angular/angular/issues/9101 // Delay the router instantiation to avoid circular dependency (ApplicationRef -> @@ -100,8 +104,7 @@ export function provideRouter(routes: Routes, config: ExtraOptions): any[] { ] }, - RouterOutletMap, - {provide: ActivatedRoute, useFactory: (r: Router) => r.routerState.root, deps: [Router]}, + RouterOutletMap, {provide: ActivatedRoute, useFactory: rootRoute, deps: [Router]}, // Trigger initial navigation {provide: APP_INITIALIZER, multi: true, useFactory: setupRouterInitializer, deps: [Injector]}, diff --git a/modules/@angular/router/src/router_module.ts b/modules/@angular/router/src/router_module.ts index b03edc34fc..259ea41574 100644 --- a/modules/@angular/router/src/router_module.ts +++ b/modules/@angular/router/src/router_module.ts @@ -9,7 +9,7 @@ import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common'; import {AppModule, AppModuleFactoryLoader, ApplicationRef, ComponentResolver, Injector, OpaqueToken, SystemJsAppModuleLoader} from '@angular/core'; -import {ROUTER_CONFIGURATION, setupRouter} from './common_router_providers'; +import {ROUTER_CONFIGURATION, rootRoute, setupRouter} from './common_router_providers'; import {RouterLink, RouterLinkWithHref} from './directives/router_link'; import {RouterLinkActive} from './directives/router_link_active'; import {RouterOutlet} from './directives/router_outlet'; @@ -20,6 +20,7 @@ import {ActivatedRoute} from './router_state'; import {DefaultUrlSerializer, UrlSerializer} from './url_tree'; + /** * @stable */ @@ -35,8 +36,7 @@ export const ROUTER_PROVIDERS: any[] = [ AppModuleFactoryLoader, ROUTES, ROUTER_CONFIGURATION ] }, - RouterOutletMap, - {provide: ActivatedRoute, useFactory: (r: Router) => r.routerState.root, deps: [Router]}, + RouterOutletMap, {provide: ActivatedRoute, useFactory: rootRoute, deps: [Router]}, {provide: AppModuleFactoryLoader, useClass: SystemJsAppModuleLoader}, {provide: ROUTER_CONFIGURATION, useValue: {enableTracing: false}} ]; diff --git a/modules/@angular/router/testing/router_testing_module.ts b/modules/@angular/router/testing/router_testing_module.ts index 1019961f6f..ba8df0c29e 100644 --- a/modules/@angular/router/testing/router_testing_module.ts +++ b/modules/@angular/router/testing/router_testing_module.ts @@ -37,6 +37,12 @@ export class SpyAppModuleFactoryLoader implements AppModuleFactoryLoader { } } +function setupTestingRouter( + resolver: ComponentResolver, urlSerializer: UrlSerializer, outletMap: RouterOutletMap, + location: Location, loader: AppModuleFactoryLoader, injector: Injector, routes: Routes) { + return new Router(null, resolver, urlSerializer, outletMap, location, injector, loader, routes); +} + /** * A module setting up the router that should be used for testing. * It provides spy implementations of Location, LocationStrategy, and AppModuleFactoryLoader. @@ -64,12 +70,7 @@ export class SpyAppModuleFactoryLoader implements AppModuleFactoryLoader { {provide: AppModuleFactoryLoader, useClass: SpyAppModuleFactoryLoader}, { provide: Router, - useFactory: (resolver: ComponentResolver, urlSerializer: UrlSerializer, - outletMap: RouterOutletMap, location: Location, loader: AppModuleFactoryLoader, - injector: Injector, routes: Routes) => { - return new Router( - null, resolver, urlSerializer, outletMap, location, injector, loader, routes); - }, + useFactory: setupTestingRouter, deps: [ ComponentResolver, UrlSerializer, RouterOutletMap, Location, AppModuleFactoryLoader, Injector, ROUTES