diff --git a/modules/@angular/router/src/router.ts b/modules/@angular/router/src/router.ts index f9088ded9a..01b7192d75 100644 --- a/modules/@angular/router/src/router.ts +++ b/modules/@angular/router/src/router.ts @@ -757,8 +757,8 @@ export class Router { }) .then( () => { - this.navigated = true; if (navigationIsSuccessful) { + this.navigated = true; this.routerEvents.next(new NavigationEnd( id, this.serializeUrl(url), this.serializeUrl(this.currentUrlTree))); resolvePromise(true); diff --git a/modules/@angular/router/test/integration.spec.ts b/modules/@angular/router/test/integration.spec.ts index 7a6b209064..596e177e61 100644 --- a/modules/@angular/router/test/integration.spec.ts +++ b/modules/@angular/router/test/integration.spec.ts @@ -1442,6 +1442,35 @@ describe('Integration', () => { }))); }); + + describe('should redirect to / when guard returns false', () => { + beforeEach(() => TestBed.configureTestingModule({ + providers: [{ + provide: 'returnFalseAndNavigate', + useFactory: (router: Router) => () => { + router.navigate(['/']); + return false; + }, + deps: [Router] + }] + })); + + it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => { + router.resetConfig([ + { + path: '', + component: SimpleCmp, + }, + {path: 'one', component: RouteCmp, canActivate: ['returnFalseAndNavigate']} + ]); + + const fixture = TestBed.createComponent(RootCmp); + router.navigateByUrl('/one'); + advance(fixture); + expect(location.path()).toEqual('/'); + expect(fixture.nativeElement).toHaveText('simple'); + }))); + }); }); describe('CanDeactivate', () => {