Skip to content

Commit 31f210b

Browse files
atscottAndrewKushnir
authored andcommitted
fix(router): Router.createUrlTree should work with any ActivatedRoute (#48508)
This change makes the `createUrlTreeFromSnapshot` added in #45877 the default and only behavior in the Router. This now addreses #42191, #38276, #22763, and #48472 without needing to create custom handling to call `createUrlTreeFromSnapshot` (since it's now called by `Router.createUrlTree`). BREAKING CHANGE: Tests which mock `ActivatedRoute` instances may need to be adjusted because Router.createUrlTree now does the right thing in more scenarios. This means that tests with invalid/incomplete ActivatedRoute mocks may behave differently than before. Additionally, tests may now navigate to a real URL where before they would navigate to the root. Ensure that tests provide expected routes to match. There is rarely production impact, but it has been found that relative navigations when using an `ActivatedRoute` that does not appear in the current router state were effectively ignored in the past. By creating the correct URLs, this sometimes resulted in different navigation behavior in the application. Most often, this happens when attempting to create a navigation that only updates query params using an empty command array, for example `router.navigate([], {relativeTo: route, queryParams: newQueryParams})`. In this case, the `relativeTo` property should be removed. PR Close #48508
1 parent 8869289 commit 31f210b

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

packages/core/test/bundling/router/bundle.golden_symbols.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@
137137
{
138138
"name": "CreateUrlTreeStrategy"
139139
},
140+
{
141+
"name": "CreateUrlTreeUsingSnapshot"
142+
},
140143
{
141144
"name": "DEFAULT_LOCALE_ID"
142145
},
@@ -299,9 +302,6 @@
299302
{
300303
"name": "LQuery_"
301304
},
302-
{
303-
"name": "LegacyCreateUrlTree"
304-
},
305305
{
306306
"name": "LifecycleHooksFeature"
307307
},
@@ -1013,9 +1013,6 @@
10131013
{
10141014
"name": "createTemplateRef"
10151015
},
1016-
{
1017-
"name": "createUrlTree"
1018-
},
10191016
{
10201017
"name": "deactivateRouteAndItsChildren"
10211018
},

packages/router/src/create_url_tree_strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class CreateUrlTreeUsingSnapshot implements CreateUrlTreeStrategy {
7070
}
7171
}
7272

73-
@Injectable({providedIn: 'root', useClass: LegacyCreateUrlTree})
73+
@Injectable({providedIn: 'root', useClass: CreateUrlTreeUsingSnapshot})
7474
export abstract class CreateUrlTreeStrategy {
7575
abstract createUrlTree(
7676
relativeTo: ActivatedRoute|null|undefined, currentState: RouterState, currentUrlTree: UrlTree,

0 commit comments

Comments
 (0)