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( export function setupRouter(
ref: ApplicationRef, resolver: ComponentResolver, urlSerializer: UrlSerializer, ref: ApplicationRef, resolver: ComponentResolver, urlSerializer: UrlSerializer,
outletMap: RouterOutletMap, location: Location, injector: Injector, outletMap: RouterOutletMap, location: Location, injector: Injector,
loader: NgModuleFactoryLoader, config: Routes, opts: ExtraOptions) { loader: NgModuleFactoryLoader, config: Routes, opts: ExtraOptions = {}) {
if (ref.componentTypes.length == 0) { if (ref.componentTypes.length == 0) {
throw new Error('Bootstrap at least one component before injecting Router.'); throw new Error('Bootstrap at least one component before injecting Router.');
} }
@ -89,7 +89,7 @@ export function setupRouterInitializer(injector: Injector) {
* *
* @deprecated use RouterModule instead * @deprecated use RouterModule instead
*/ */
export function provideRouter(routes: Routes, config: ExtraOptions): any[] { export function provideRouter(routes: Routes, config: ExtraOptions = {}): any[] {
return [ return [
{provide: ANALYZE_FOR_ENTRY_COMPONENTS, multi: true, useValue: routes}, {provide: ANALYZE_FOR_ENTRY_COMPONENTS, multi: true, useValue: routes},
{provide: ROUTES, useExisting: ROUTER_CONFIG}, {provide: ROUTER_CONFIG, 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 * 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 {ApplicationRef, ComponentResolver, Injector, ModuleWithProviders, NgModule, NgModuleFactoryLoader, OpaqueToken, SystemJsNgModuleLoader} from '@angular/core';
import {ExtraOptions, ROUTER_CONFIGURATION, provideRouterConfig, provideRoutes, rootRoute, setupRouter} from './common_router_providers'; import {ExtraOptions, ROUTER_CONFIGURATION, provideRouterConfig, provideRoutes, rootRoute, setupRouter} from './common_router_providers';
@ -94,8 +94,12 @@ export class RouterModule {
return { return {
ngModule: RouterModule, ngModule: RouterModule,
providers: [ providers: [
ROUTER_PROVIDERS, provideRoutes(routes), config ? provideRouterConfig(config) : [], ROUTER_PROVIDERS, provideRoutes(routes), {provide: ROUTER_CONFIGURATION, useValue: config},
config.useHash ? hashLocationStrategy : pathLocationStrategy {
provide: LocationStrategy,
useFactory: provideLocationStrategy,
deps: [PlatformLocation, APP_BASE_HREF, ROUTER_CONFIGURATION]
}
] ]
}; };
} }
@ -104,3 +108,9 @@ export class RouterModule {
return {ngModule: RouterModule, providers: [provideRoutes(routes)]}; 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);
}