fix(router): make router provides work with cli and offline compilation

This commit is contained in:
vsavkin 2016-07-25 16:10:10 -07:00
parent c87847974a
commit d15a1d64e1
3 changed files with 15 additions and 11 deletions

View File

@ -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]},

View File

@ -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}}
];

View File

@ -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