diff --git a/modules/@angular/router/src/common_router_providers.ts b/modules/@angular/router/src/common_router_providers.ts index 153b7ab664..328e66afa5 100644 --- a/modules/@angular/router/src/common_router_providers.ts +++ b/modules/@angular/router/src/common_router_providers.ts @@ -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}, diff --git a/modules/@angular/router/src/router_module.ts b/modules/@angular/router/src/router_module.ts index 03884d2ea9..5bc03a4643 100644 --- a/modules/@angular/router/src/router_module.ts +++ b/modules/@angular/router/src/router_module.ts @@ -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); +} \ No newline at end of file