diff --git a/modules/@angular/router/src/create_url_tree.ts b/modules/@angular/router/src/create_url_tree.ts index 71b94f45af..6e7d449d3d 100644 --- a/modules/@angular/router/src/create_url_tree.ts +++ b/modules/@angular/router/src/create_url_tree.ts @@ -19,6 +19,8 @@ export function createUrlTree( } const normalizedCommands = normalizeCommands(commands); + validateCommands(normalizedCommands); + if (navigateToRoot(normalizedCommands)) { return tree(urlTree.root, new UrlSegment([], {}), urlTree, queryParams, fragment); } @@ -31,6 +33,12 @@ export function createUrlTree( return tree(startingPosition.segment, segment, urlTree, queryParams, fragment); } +function validateCommands(n: NormalizedNavigationCommands): void { + if (n.isAbsolute && n.commands.length > 0 && (typeof n.commands[0] === 'object')) { + throw new Error('Root segment cannot have matrix parameters'); + } +} + function tree( oldSegment: UrlSegment, newSegment: UrlSegment, urlTree: UrlTree, queryParams: Params, fragment: string): UrlTree { diff --git a/modules/@angular/router/test/create_url_tree.spec.ts b/modules/@angular/router/test/create_url_tree.spec.ts index b1ef171672..5537c0736d 100644 --- a/modules/@angular/router/test/create_url_tree.spec.ts +++ b/modules/@angular/router/test/create_url_tree.spec.ts @@ -14,13 +14,18 @@ describe('createUrlTree', () => { expect(serializer.serialize(t)).toEqual('/'); }); + it('should error when navigating to the root segment with params', () => { + const p = serializer.parse('/'); + expect(() => createRoot(p, ['/', {p: 11}])) + .toThrowError(/Root segment cannot have matrix parameters/); + }); + it('should support nested segments', () => { const p = serializer.parse('/a/b'); const t = createRoot(p, ['/one', 11, 'two', 22]); expect(serializer.serialize(t)).toEqual('/one/11/two/22'); }); - it('should stringify positional parameters', () => { const p = serializer.parse('/a/b'); const t = createRoot(p, ['/one', 11]);