From db867fee77bc62f367fc5d484fc3951d72d998c8 Mon Sep 17 00:00:00 2001 From: Albert Szekely Date: Sun, 30 Oct 2022 14:43:33 +0200 Subject: [PATCH] fix(router): fix redirectTo on named outlets - resolves #33783 (#47927) fix(router): fix redirectTo on named outlets - resolves #33783 PR Close #47927 --- packages/router/src/utils/config.ts | 4 ++-- packages/router/test/config.spec.ts | 6 ++++++ packages/router/test/integration.spec.ts | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/router/src/utils/config.ts b/packages/router/src/utils/config.ts index 88a8a8bf22059..5e674df8ae0e0 100644 --- a/packages/router/src/utils/config.ts +++ b/packages/router/src/utils/config.ts @@ -84,8 +84,8 @@ function validateNode(route: Route, fullPath: string, requireStandaloneComponent RuntimeErrorCode.INVALID_ROUTE_CONFIG, `Invalid configuration of route '${fullPath}': Array cannot be specified`); } - if (!route.component && !route.loadComponent && !route.children && !route.loadChildren && - (route.outlet && route.outlet !== PRIMARY_OUTLET)) { + if (!route.redirectTo && !route.component && !route.loadComponent && !route.children && + !route.loadChildren && (route.outlet && route.outlet !== PRIMARY_OUTLET)) { throw new RuntimeError( RuntimeErrorCode.INVALID_ROUTE_CONFIG, `Invalid configuration of route '${ diff --git a/packages/router/test/config.spec.ts b/packages/router/test/config.spec.ts index 9fbc5413ece85..28a60db1333ec 100644 --- a/packages/router/test/config.spec.ts +++ b/packages/router/test/config.spec.ts @@ -157,6 +157,12 @@ describe('config', () => { validateConfig([{path: 'a', outlet: 'aux', loadChildren: jasmine.createSpy('child')}]); }).not.toThrow(); }); + + it('should not throw when outlet has redirectTo', () => { + expect(() => { + validateConfig([{path: '', pathMatch: 'prefix', outlet: 'aux', redirectTo: 'main'}]); + }).not.toThrow(); + }); }); }); diff --git a/packages/router/test/integration.spec.ts b/packages/router/test/integration.spec.ts index 6e11e68561358..9ca4f39e272df 100644 --- a/packages/router/test/integration.spec.ts +++ b/packages/router/test/integration.spec.ts @@ -3006,6 +3006,22 @@ describe('Integration', () => { expect(history[history.length - 1].state) .toEqual({foo: 'bar', navigationId: history.length}); }))); + + + it('can redirect from componentless named outlets', fakeAsync(() => { + const router = TestBed.inject(Router); + const fixture = createRoot(router, RootCmp); + + router.resetConfig([ + {path: 'main', outlet: 'aux', component: BlankCmp}, + {path: '', pathMatch: 'full', outlet: 'aux', redirectTo: 'main'}, + ]); + + router.navigateByUrl(''); + advance(fixture); + + expect(TestBed.inject(Location).path()).toEqual('/(aux:main)'); + })); }); it('should set href on area elements', fakeAsync(() => {