fix(router): updates router module to be offline-compilation friendly

This commit is contained in:
vsavkin 2016-07-28 12:10:36 -07:00
parent 7c76a75452
commit 72da547d6a
2 changed files with 15 additions and 5 deletions

View File

@ -29,7 +29,7 @@ export interface ExtraOptions {
export function setupRouter(
ref: ApplicationRef, resolver: ComponentResolver, urlSerializer: UrlSerializer,
outletMap: RouterOutletMap, location: Location, injector: Injector,
loader: NgModuleFactoryLoader, config: Routes, opts: ExtraOptions) {
loader: NgModuleFactoryLoader, config: Routes, opts: ExtraOptions = {}) {
if (ref.componentTypes.length == 0) {
throw new Error('Bootstrap at least one component before injecting Router.');
}
@ -89,7 +89,7 @@ export function setupRouterInitializer(injector: Injector) {
*
* @deprecated use RouterModule instead
*/
export function provideRouter(routes: Routes, config: ExtraOptions): any[] {
export function provideRouter(routes: Routes, config: ExtraOptions = {}): any[] {
return [
{provide: ANALYZE_FOR_ENTRY_COMPONENTS, multi: true, useValue: routes},
{provide: ROUTES, useExisting: ROUTER_CONFIG}, {provide: ROUTER_CONFIG, useValue: routes},

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {HashLocationStrategy, Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
import {APP_BASE_HREF, HashLocationStrategy, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common';
import {ApplicationRef, ComponentResolver, Injector, ModuleWithProviders, NgModule, NgModuleFactoryLoader, OpaqueToken, SystemJsNgModuleLoader} from '@angular/core';
import {ExtraOptions, ROUTER_CONFIGURATION, provideRouterConfig, provideRoutes, rootRoute, setupRouter} from './common_router_providers';
@ -94,8 +94,12 @@ export class RouterModule {
return {
ngModule: RouterModule,
providers: [
ROUTER_PROVIDERS, provideRoutes(routes), config ? provideRouterConfig(config) : [],
config.useHash ? hashLocationStrategy : pathLocationStrategy
ROUTER_PROVIDERS, provideRoutes(routes), {provide: ROUTER_CONFIGURATION, useValue: config},
{
provide: LocationStrategy,
useFactory: provideLocationStrategy,
deps: [PlatformLocation, APP_BASE_HREF, ROUTER_CONFIGURATION]
}
]
};
}
@ -104,3 +108,9 @@ export class RouterModule {
return {ngModule: RouterModule, providers: [provideRoutes(routes)]};
}
}
function provideLocationStrategy(
platformLocationStrategy: PlatformLocation, baseHref: string, options: ExtraOptions = {}) {
return options.useHash ? new HashLocationStrategy(platformLocationStrategy, baseHref) :
new PathLocationStrategy(platformLocationStrategy, baseHref);
}