MCPcopy
hub / github.com/angular/angular / createUrlTree

Method createUrlTree

packages/router/src/router.ts:462–510  ·  view source on GitHub ↗

* Appends URL segments to the current URL tree to create a new URL tree. * * @param commands An array of URL fragments with which to construct the new URL tree. * If the path is static, can be the literal URL string. For a dynamic path, pass an array of path * segments, followed by the p

(commands: readonly any[], navigationExtras: UrlCreationOptions = {})

Source from the content-addressed store, hash-verified

460 *
461 */
462 createUrlTree(commands: readonly any[], navigationExtras: UrlCreationOptions = {}): UrlTree {
463 const {relativeTo, queryParams, fragment, queryParamsHandling, preserveFragment} =
464 navigationExtras;
465 const f = preserveFragment ? this.currentUrlTree.fragment : fragment;
466 let q: Params | null = null;
467 switch (queryParamsHandling ?? this.options.defaultQueryParamsHandling) {
468 case 'merge':
469 q = {...this.currentUrlTree.queryParams, ...queryParams};
470 break;
471 case 'preserve':
472 q = this.currentUrlTree.queryParams;
473 break;
474 default:
475 q = queryParams || null;
476 }
477 if (q !== null) {
478 q = this.removeEmptyProps(q);
479 }
480
481 let relativeToUrlSegmentGroup: UrlSegmentGroup | undefined;
482 try {
483 const relativeToSnapshot = relativeTo ? relativeTo.snapshot : this.routerState.snapshot.root;
484 relativeToUrlSegmentGroup = createSegmentGroupFromRoute(relativeToSnapshot);
485 } catch (e: unknown) {
486 // This is strictly for backwards compatibility with tests that create
487 // invalid `ActivatedRoute` mocks.
488 // Note: the difference between having this fallback for invalid `ActivatedRoute` setups and
489 // just throwing is ~500 test failures. Fixing all of those tests by hand is not feasible at
490 // the moment.
491 if (typeof commands[0] !== 'string' || commands[0][0] !== '/') {
492 // Navigations that were absolute in the old way of creating UrlTrees
493 // would still work because they wouldn't attempt to match the
494 // segments in the `ActivatedRoute` to the `currentUrlTree` but
495 // instead just replace the root segment with the navigation result.
496 // Non-absolute navigations would fail to apply the commands because
497 // the logic could not find the segment to replace (so they'd act like there were no
498 // commands).
499 commands = [];
500 }
501 relativeToUrlSegmentGroup = this.currentUrlTree.root;
502 }
503 return createUrlTreeFromSegmentGroup(
504 relativeToUrlSegmentGroup,
505 commands,
506 q,
507 f ?? null,
508 this.urlSerializer,
509 );
510 }
511
512 /**
513 * Navigates to a view using an absolute route path.

Callers 13

navigateMethod · 0.95
url_tree.spec.tsFile · 0.80
WithUrlTreeClass · 0.80
createRootFunction · 0.80
createFunction · 0.80
TestModuleClass · 0.80
guardsIntegrationSuiteFunction · 0.80
canMatchFactoryFunction · 0.80

Calls 3

removeEmptyPropsMethod · 0.95

Tested by 4

createRootFunction · 0.64
createFunction · 0.64
guardsIntegrationSuiteFunction · 0.64
canMatchFactoryFunction · 0.64