Skip to content

Commit

Permalink
fix(router): fix navigation from the root component ngOnInit hook (#1…
Browse files Browse the repository at this point in the history
…3932)

Closes #13795

PR Close #13932
  • Loading branch information
Dzmitry Shylovich authored and mhevery committed Feb 3, 2017
1 parent a047124 commit 4d2901d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion modules/@angular/router/src/router.ts
Expand Up @@ -364,7 +364,9 @@ export class Router {
*/
initialNavigation(): void {
this.setUpLocationChangeListener();
this.navigateByUrl(this.location.path(true), {replaceUrl: true});
if (this.navigationId === 0) {
this.navigateByUrl(this.location.path(true), {replaceUrl: true});
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions modules/@angular/router/test/integration.spec.ts
Expand Up @@ -37,6 +37,18 @@ describe('Integration', () => {
expect(location.path()).toEqual('/simple');
})));

it('should navigate from ngOnInit hook',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
router.resetConfig([
{path: '', component: SimpleCmp},
{path: 'one', component: RouteCmp},
]);

const fixture = createRoot(router, RootCmpWithOnInit);
expect(location.path()).toEqual('/one');
expect(fixture.nativeElement).toHaveText('route');
})));

describe('should execute navigations serially', () => {
let log: any[] = [];

Expand Down Expand Up @@ -2868,6 +2880,13 @@ class ComponentRecordingRoutePathAndUrl {
class RootCmp {
}

@Component({selector: 'root-cmp-on-init', template: `<router-outlet></router-outlet>`})
class RootCmpWithOnInit {
constructor(private router: Router) {}

ngOnInit(): void { this.router.navigate(['one']); }
}

@Component({
selector: 'root-cmp',
template:
Expand Down Expand Up @@ -2939,6 +2958,7 @@ function createRoot(router: Router, type: any): ComponentFixture<any> {
ComponentRecordingRoutePathAndUrl,
RouteCmp,
RootCmp,
RootCmpWithOnInit,
RelativeLinkInIfCmp,
RootCmpWithTwoOutlets,
EmptyQueryParamsCmp,
Expand Down Expand Up @@ -2966,6 +2986,7 @@ function createRoot(router: Router, type: any): ComponentFixture<any> {
ComponentRecordingRoutePathAndUrl,
RouteCmp,
RootCmp,
RootCmpWithOnInit,
RelativeLinkInIfCmp,
RootCmpWithTwoOutlets,
EmptyQueryParamsCmp,
Expand Down

0 comments on commit 4d2901d

Please sign in to comment.