feat(router): Allow Route.redirectTo to be a function which returns a… - #52606
Closed
atscott wants to merge 2 commits into
Closed
feat(router): Allow Route.redirectTo to be a function which returns a…#52606atscott wants to merge 2 commits into
atscott wants to merge 2 commits into
Conversation
atscott
force-pushed
the
redirectFunction
branch
3 times, most recently
from
November 13, 2023 17:17
0ee8cbc to
62c3106
Compare
Contributor
Author
|
Updating target to |
atscott
force-pushed
the
redirectFunction
branch
from
February 16, 2024 01:12
62c3106 to
3de31e5
Compare
atscott
force-pushed
the
redirectFunction
branch
from
February 16, 2024 01:14
3de31e5 to
80229ff
Compare
atscott
force-pushed
the
redirectFunction
branch
2 times, most recently
from
February 20, 2024 18:14
a4322b7 to
a9b79ac
Compare
atscott
marked this pull request as ready for review
February 20, 2024 18:14
pullapprove
Bot
requested review from
AndrewKushnir,
alxhub and
jessicajaniuk
February 20, 2024 18:15
AndrewKushnir
approved these changes
Feb 20, 2024
atscott
force-pushed
the
redirectFunction
branch
from
February 21, 2024 00:41
a9b79ac to
607c901
Compare
jessicajaniuk
approved these changes
Feb 21, 2024
jessicajaniuk
left a comment
Contributor
There was a problem hiding this comment.
reviewed-for: public-api
atscott
force-pushed
the
redirectFunction
branch
2 times, most recently
from
March 12, 2024 16:30
7992b66 to
53790be
Compare
… string or UrlTree
This commit updates the logic around `Route.redirectTo` to enable using a
function to create the redirect. This function can return a string,
ands acts the same as previous string redirects, or a `UrlTree`, which
will act as an absolute redirect.
To be useful, the redirect function needs access to the params and data.
Today, developers can access these in their redirect strings, for
example `{path: ':id', redirectTo: '/user/:id'}`. Unfortunately,
developers only have access to params and data on the _current route_
today in the redirect strings. The params and data in the `RedirectFn`
give developers access to anything from the matched parent routes as
well. This is done as the same way as param and data aggregation later
on (https://github.com/angular/angular/blob/897f014785578d87bc655ea6ae9e113653960f50/packages/router/src/router_state.ts#L236-L278).
In order to accomplish this, we inherit params and data while
matching, after the `ActivatedRouteSnapshot` is created for the matched
route rather than waiting until the end.
The `RedirectFunction` does not return the full
`ActivatedRouteSnapshot` interface. Some things are not accurately known
at the route matching phase. For example, resolvers are not run until
later, so any resolved title would not be populated. The same goes for lazy
loaded components. The is also true for all the snapshots up to the
root, so properties that include parents (root, parent, pathFromRoot)
are also excluded. And naturally, the full route matching hasn't yet
happened so firstChild and children are not available either.
fixes angular#13373
resolves angular#28661 (though not for the redirect string - you would return a
`UrlTree` from the function)
BREAKING CHANGE: This change allows `Route.redirectTo` to be a function
in addition to the previous string. Code which expects `redirectTo` to
only be a string on `Route` objects will need to be adjusted.
atscott
force-pushed
the
redirectFunction
branch
2 times, most recently
from
March 14, 2024 16:30
08e0f2c to
4a846ed
Compare
Contributor
Author
atscott
force-pushed
the
redirectFunction
branch
from
March 14, 2024 16:53
4a846ed to
e68eb09
Compare
…turns a string or UrlTree
atscott
force-pushed
the
redirectFunction
branch
from
March 14, 2024 17:32
e68eb09 to
41e2c61
Compare
Contributor
Author
|
This PR was merged into the repository by commit 2b80258. |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
… string or UrlTree
This commit updates the logic around
Route.redirectToto enable using a function to create the redirect. This function can return a string, ands acts the same as previous string redirects, or aUrlTree, which will act as an absolute redirect.To be useful, the redirect function needs access to the params and data. Today, developers can access these in their redirect strings, for example
{path: ':id', redirectTo: '/user/:id'}. Unfortunately, developers only have access to params and data on the current route today in the redirect strings. The params and data in theRedirectFngive developers access to anything from the matched parent routes as well. This is done as the same way as param and data aggregation later on (angular/packages/router/src/router_state.ts
Lines 236 to 278 in 897f014
ActivatedRouteSnapshotis created for the matched route rather than waiting until the end.The
RedirectFunctiondoes not return the fullActivatedRouteSnapshotinterface. Some things are not accurately known at the route matching phase. For example, resolvers are not run until later, so any resolved title would not be populated. The same goes for lazy loaded components. The is also true for all the snapshots up to the root, so properties that include parents (root, parent, pathFromRoot) are also excluded. And naturally, the full route matching hasn't yet happened so firstChild and children are not available either.fixes #13373
resolves #28661 (though not for the redirect string - you would return a
UrlTreefrom the function)