Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit da5fc69

Browse files
muceralxhub
authored andcommitted
fix(router): do not update primary route if only secondary outlet is given (angular#11797)
1 parent b44b6ef commit da5fc69

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

modules/@angular/router/src/create_url_tree.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ function createPositionApplyingDoubleDots(
182182
}
183183

184184
function getPath(command: any): any {
185+
if (typeof command === 'object' && command.outlets) return command.outlets[PRIMARY_OUTLET];
185186
return `${command}`;
186187
}
187188

@@ -201,9 +202,13 @@ function updateSegmentGroup(
201202
}
202203

203204
const m = prefixedWith(segmentGroup, startIndex, commands);
204-
const slicedCommands = commands.slice(m.lastIndex);
205-
206-
if (m.match && slicedCommands.length === 0) {
205+
const slicedCommands = commands.slice(m.commandIndex);
206+
if (m.match && m.pathIndex < segmentGroup.segments.length) {
207+
var g = new UrlSegmentGroup(segmentGroup.segments.slice(0, m.pathIndex), {});
208+
g.children[PRIMARY_OUTLET] =
209+
new UrlSegmentGroup(segmentGroup.segments.slice(m.pathIndex), segmentGroup.children);
210+
return updateSegmentGroupChildren(g, 0, slicedCommands);
211+
} else if (m.match && slicedCommands.length === 0) {
207212
return new UrlSegmentGroup(segmentGroup.segments, {});
208213
} else if (m.match && !segmentGroup.hasChildren()) {
209214
return createNewSegmentGroup(segmentGroup, startIndex, commands);
@@ -241,14 +246,16 @@ function prefixedWith(segmentGroup: UrlSegmentGroup, startIndex: number, command
241246
let currentCommandIndex = 0;
242247
let currentPathIndex = startIndex;
243248

244-
const noMatch = {match: false, lastIndex: 0};
249+
const noMatch = {match: false, pathIndex: 0, commandIndex: 0};
245250
while (currentPathIndex < segmentGroup.segments.length) {
246251
if (currentCommandIndex >= commands.length) return noMatch;
247252
const path = segmentGroup.segments[currentPathIndex];
248253
const curr = getPath(commands[currentCommandIndex]);
249254
const next =
250255
currentCommandIndex < commands.length - 1 ? commands[currentCommandIndex + 1] : null;
251256

257+
if (currentPathIndex > 0 && curr === undefined) break;
258+
252259
if (curr && next && (typeof next === 'object') && next.outlets === undefined) {
253260
if (!compare(curr, next, path)) return noMatch;
254261
currentCommandIndex += 2;
@@ -259,7 +266,7 @@ function prefixedWith(segmentGroup: UrlSegmentGroup, startIndex: number, command
259266
currentPathIndex++;
260267
}
261268

262-
return {match: true, lastIndex: currentCommandIndex};
269+
return {match: true, pathIndex: currentPathIndex, commandIndex: currentCommandIndex};
263270
}
264271

265272
function createNewSegmentGroup(

modules/@angular/router/test/integration.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,27 @@ describe('Integration', () => {
264264
expect(fixture.nativeElement).toHaveText('team 22 [ user victor, right: simple ]');
265265
})));
266266

267+
it('should support secondary routes in seperate commands',
268+
fakeAsync(inject([Router], (router: Router) => {
269+
const fixture = createRoot(router, RootCmp);
270+
271+
router.resetConfig([{
272+
path: 'team/:id',
273+
component: TeamCmp,
274+
children: [
275+
{path: 'user/:name', component: UserCmp},
276+
{path: 'simple', component: SimpleCmp, outlet: 'right'}
277+
]
278+
}]);
279+
280+
router.navigateByUrl('/team/22/user/victor');
281+
advance(fixture);
282+
router.navigate(['team/22', {outlets: {right: 'simple'}}]);
283+
advance(fixture);
284+
285+
expect(fixture.nativeElement).toHaveText('team 22 [ user victor, right: simple ]');
286+
})));
287+
267288
it('should deactivate outlets', fakeAsync(inject([Router], (router: Router) => {
268289
const fixture = createRoot(router, RootCmp);
269290

0 commit comments

Comments
 (0)