fix(router): add an app initializer to trigger initial navigation

Closes #10
This commit is contained in:
vsavkin 2016-06-06 14:05:57 -07:00
parent 5742d4720a
commit b0e7c14545
3 changed files with 13 additions and 4 deletions

View File

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

View File

@ -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<Event>();
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());
}

View File

@ -307,7 +307,7 @@ describe("Integration", () => {
[NavigationEnd, '/user/fedor']
]);
})));
describe("router links", () => {
it("should support string router links",
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {