-
Notifications
You must be signed in to change notification settings - Fork 26.5k
feat(router): Allow Route.redirectTo to be a function which returns a… #52606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
0ee8cbc
to
62c3106
Compare
Updating target to |
62c3106
to
3de31e5
Compare
3de31e5
to
80229ff
Compare
a4322b7
to
a9b79ac
Compare
a9b79ac
to
607c901
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reviewed-for: public-api
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.
08e0f2c
to
4a846ed
Compare
4a846ed
to
e68eb09
Compare
…turns a string or UrlTree
e68eb09
to
41e2c61
Compare
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. |
… 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 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 theRedirectFn
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 (angular/packages/router/src/router_state.ts
Lines 236 to 278 in 897f014
ActivatedRouteSnapshot
is created for the matched route rather than waiting until the end.The
RedirectFunction
does not return the fullActivatedRouteSnapshot
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 #13373
resolves #28661 (though not for the redirect string - you would return a
UrlTree
from the function)