fix(router): add an app initializer to trigger initial navigation
Closes #10
This commit is contained in:
parent
5742d4720a
commit
b0e7c14545
@ -3,7 +3,7 @@ import { UrlSerializer, DefaultUrlSerializer } from './url_serializer';
|
|||||||
import { ActivatedRoute } from './router_state';
|
import { ActivatedRoute } from './router_state';
|
||||||
import { Router } from './router';
|
import { Router } from './router';
|
||||||
import { RouterConfig } from './config';
|
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';
|
import { LocationStrategy, PathLocationStrategy, Location } from '@angular/common';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,5 +46,8 @@ export function provideRouter(config: RouterConfig):any[] {
|
|||||||
|
|
||||||
RouterOutletMap,
|
RouterOutletMap,
|
||||||
{provide: ActivatedRoute, useFactory: (r) => r.routerState.root, deps: [Router]},
|
{provide: ActivatedRoute, useFactory: (r) => r.routerState.root, deps: [Router]},
|
||||||
|
|
||||||
|
// Trigger initial navigation
|
||||||
|
{provide: APP_INITIALIZER, multi: true, useFactory: (router: Router) => router.initialNavigation(), deps: [Router]},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
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.routerEvents = new Subject<Event>();
|
||||||
this.currentUrlTree = createEmptyUrlTree();
|
this.currentUrlTree = createEmptyUrlTree()
|
||||||
this.currentRouterState = createEmptyState(rootComponentType);
|
this.currentRouterState = createEmptyState(this.rootComponentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
initialNavigation():void {
|
||||||
this.setUpLocationChangeListener();
|
this.setUpLocationChangeListener();
|
||||||
this.navigateByUrl(this.location.path());
|
this.navigateByUrl(this.location.path());
|
||||||
}
|
}
|
||||||
|
@ -307,7 +307,7 @@ describe("Integration", () => {
|
|||||||
[NavigationEnd, '/user/fedor']
|
[NavigationEnd, '/user/fedor']
|
||||||
]);
|
]);
|
||||||
})));
|
})));
|
||||||
|
|
||||||
describe("router links", () => {
|
describe("router links", () => {
|
||||||
it("should support string router links",
|
it("should support string router links",
|
||||||
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user