From b0e7c1454524fc4deacca0b9e83cfc2bdcff800d Mon Sep 17 00:00:00 2001 From: vsavkin Date: Mon, 6 Jun 2016 14:05:57 -0700 Subject: [PATCH] fix(router): add an app initializer to trigger initial navigation Closes #10 --- modules/@angular/router/src/common_router_providers.ts | 5 ++++- modules/@angular/router/src/router.ts | 10 ++++++++-- modules/@angular/router/test/router.spec.ts | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/modules/@angular/router/src/common_router_providers.ts b/modules/@angular/router/src/common_router_providers.ts index 6442df813f..7c25921611 100644 --- a/modules/@angular/router/src/common_router_providers.ts +++ b/modules/@angular/router/src/common_router_providers.ts @@ -3,7 +3,7 @@ import { UrlSerializer, DefaultUrlSerializer } from './url_serializer'; import { ActivatedRoute } from './router_state'; import { Router } from './router'; import { RouterConfig } from './config'; -import { ComponentResolver, ApplicationRef, Injector } from '@angular/core'; +import { ComponentResolver, ApplicationRef, Injector, APP_INITIALIZER } from '@angular/core'; import { LocationStrategy, PathLocationStrategy, Location } from '@angular/common'; /** @@ -46,5 +46,8 @@ export function provideRouter(config: RouterConfig):any[] { RouterOutletMap, {provide: ActivatedRoute, useFactory: (r) => r.routerState.root, deps: [Router]}, + + // Trigger initial navigation + {provide: APP_INITIALIZER, multi: true, useFactory: (router: Router) => router.initialNavigation(), deps: [Router]}, ]; } diff --git a/modules/@angular/router/src/router.ts b/modules/@angular/router/src/router.ts index 89b73fa604..3c26f43ff8 100644 --- a/modules/@angular/router/src/router.ts +++ b/modules/@angular/router/src/router.ts @@ -63,8 +63,14 @@ export class Router { */ constructor(private rootComponentType:Type, private resolver: ComponentResolver, private urlSerializer: UrlSerializer, private outletMap: RouterOutletMap, private location: Location, private injector: Injector, private config: RouterConfig) { this.routerEvents = new Subject(); - this.currentUrlTree = createEmptyUrlTree(); - this.currentRouterState = createEmptyState(rootComponentType); + this.currentUrlTree = createEmptyUrlTree() + this.currentRouterState = createEmptyState(this.rootComponentType); + } + + /** + * @internal + */ + initialNavigation():void { this.setUpLocationChangeListener(); this.navigateByUrl(this.location.path()); } diff --git a/modules/@angular/router/test/router.spec.ts b/modules/@angular/router/test/router.spec.ts index 194ac9d362..92b2010325 100644 --- a/modules/@angular/router/test/router.spec.ts +++ b/modules/@angular/router/test/router.spec.ts @@ -307,7 +307,7 @@ describe("Integration", () => { [NavigationEnd, '/user/fedor'] ]); }))); - + describe("router links", () => { it("should support string router links", fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {