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

Method createUrlTree

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

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

Callers 14

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

Calls 2

Tested by 4

createRootFunction · 0.36
createFunction · 0.36
guardsIntegrationSuiteFunction · 0.36
canMatchFactoryFunction · 0.36