MCPcopy Create free account
hub / github.com/angular/angular / createUrlTree

Method createUrlTree

packages/router/src/router.ts:472–520  ·  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

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

Callers 15

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

Calls 3

removeEmptyPropsMethod · 0.95

Tested by 4

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