fix(router): fix navigation from the root component ngOnInit hook (#13932)
Closes #13795 PR Close #13932
This commit is contained in:
parent
e075b1ba83
commit
47d41d492b
|
@ -364,8 +364,10 @@ export class Router {
|
||||||
*/
|
*/
|
||||||
initialNavigation(): void {
|
initialNavigation(): void {
|
||||||
this.setUpLocationChangeListener();
|
this.setUpLocationChangeListener();
|
||||||
|
if (this.navigationId === 0) {
|
||||||
this.navigateByUrl(this.location.path(true), {replaceUrl: true});
|
this.navigateByUrl(this.location.path(true), {replaceUrl: true});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up the location change listener.
|
* Sets up the location change listener.
|
||||||
|
|
|
@ -37,6 +37,18 @@ describe('Integration', () => {
|
||||||
expect(location.path()).toEqual('/simple');
|
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', () => {
|
describe('should execute navigations serially', () => {
|
||||||
let log: any[] = [];
|
let log: any[] = [];
|
||||||
|
|
||||||
|
@ -2932,6 +2944,13 @@ class ComponentRecordingRoutePathAndUrl {
|
||||||
class RootCmp {
|
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({
|
@Component({
|
||||||
selector: 'root-cmp',
|
selector: 'root-cmp',
|
||||||
template:
|
template:
|
||||||
|
@ -3003,6 +3022,7 @@ function createRoot(router: Router, type: any): ComponentFixture<any> {
|
||||||
ComponentRecordingRoutePathAndUrl,
|
ComponentRecordingRoutePathAndUrl,
|
||||||
RouteCmp,
|
RouteCmp,
|
||||||
RootCmp,
|
RootCmp,
|
||||||
|
RootCmpWithOnInit,
|
||||||
RelativeLinkInIfCmp,
|
RelativeLinkInIfCmp,
|
||||||
RootCmpWithTwoOutlets,
|
RootCmpWithTwoOutlets,
|
||||||
EmptyQueryParamsCmp,
|
EmptyQueryParamsCmp,
|
||||||
|
@ -3030,6 +3050,7 @@ function createRoot(router: Router, type: any): ComponentFixture<any> {
|
||||||
ComponentRecordingRoutePathAndUrl,
|
ComponentRecordingRoutePathAndUrl,
|
||||||
RouteCmp,
|
RouteCmp,
|
||||||
RootCmp,
|
RootCmp,
|
||||||
|
RootCmpWithOnInit,
|
||||||
RelativeLinkInIfCmp,
|
RelativeLinkInIfCmp,
|
||||||
RootCmpWithTwoOutlets,
|
RootCmpWithTwoOutlets,
|
||||||
EmptyQueryParamsCmp,
|
EmptyQueryParamsCmp,
|
||||||
|
|
Loading…
Reference in New Issue