Title: Router Resolve
Open Graph Title: Router Resolve
X Title: Router Resolve
Description: 🚀 feature request Relevant Package @angular router Description I see that in the docs it is recommended to do the following to cancel a navigation event in a Resolve
Open Graph Description: 🚀 feature request Relevant Package @angular router Description I see that in the docs it is recommended to do the following to cancel a navigation event in a Resolve
X Description: 🚀 feature request Relevant Package @angular router Description I see that in the docs it is recommended to do the following to cancel a navigation event in a Resolve<T> route data resolver. r...
Opengraph URL: https://github.com/angular/angular/issues/29089
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Router Resolve\u003cT\u003e should have official support for UrlTree based redirection","articleBody":"# 🚀 feature request\r\n\r\n### Relevant Package\r\n@angular router\r\n\r\n### Description\r\nI see that in the [docs](https://angular.io/guide/router#!#resolve-guard) it is recommended to do the following to cancel a navigation event in a `Resolve\u003cT\u003e` route data resolver.\r\n\r\n```\r\nresolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable\u003cCrisis\u003e | Observable\u003cnever\u003e {\r\n let id = route.paramMap.get('id');\r\n \r\n return this.cs.getCrisis(id).pipe(\r\n take(1),\r\n mergeMap(crisis =\u003e {\r\n if (crisis) {\r\n return of(crisis);\r\n } else { // id not found\r\n this.router.navigate(['/crisis-center']);\r\n return EMPTY;\r\n }\r\n })\r\n );\r\n }\r\n```\r\n While this does work it generates a pretty gnarly `NavigationCancel` event that basically says 'You broke my internal state':\r\n\r\n```\r\nNavigationCancel {id: 5, url: \"/crisis-center/4444\", reason: \"Navigation ID 5 is not equal to the current navigation id 6\"}\r\nid: 5\r\nreason: \"Navigation ID 5 is not equal to the current navigation id 6\"\r\nurl: \"/crisis-center/4444\"\r\n```\r\n\r\nThis is because we have overlapping navigation events, and the original navigation is cancelled because we essentially triggered the router's panic mode.\r\n\r\n### Describe the solution you'd like\r\nAngular 7.1 introduced the option of returning a `UrlTree` from a `canActivate`, and I think something similar should be added to `Resolve\u003cT\u003e` for feature parity.\r\n\r\nA `Resolve` may fail for many reasons, but we should have a clean option to redirect within the resolver - especially in cases where it is impossible to know the final destination until you've attempted to get the data.\r\n\r\nI think perhaps the signature of `resolve` needs to become something like :\r\n\r\n resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable\u003cT | UrlTree\u003e | Promise\u003cT | UrlTree\u003e | T | UrlTree;\r\n\r\n### Describe alternatives you've considered\r\nYes, delving into the code showed me that I could throw an error that satisfies the `isNavigationCancelingError()` type-guard and not only would it give me a nicer `NavigationCancel` event it would also allow me to pass a `url: UrlTree` parameter which will then be navigated to on my behalf.\r\n\r\n throw { ngNavigationCancelingError: true, url: this.router.createUrlTree(newUrl) };\r\n\r\nWhile this actually works quite well - and gives the following 'nice' event it isn't really supported since I have to use the magic internal property name - and also not every reason for aborting a Resolve is an error condition.\r\n\r\n```\r\nNavigationCancel {id: 11, url: \"/account/orders/latest\", reason: undefined}\r\nid: 11\r\nreason: undefined\r\nurl: \"/account/orders/latest\"\r\n```\r\n\r\nThe other alternative is what is suggested in the docs, but the error (`Navigation ID 5 is not equal to the current navigation id 6`) from doing that just makes it look like something is broken. \r\n\r\nI think the mechanics of this are basically already there, expecially with the new functionality in 7.1 to handle a `UrlTree`. There is also plenty of need for [such a feature](https://stackoverflow.com/questions/43097103/redirect-if-resolve-fails-angular-2) on Stackoverflow..","author":{"url":"https://github.com/simeyla","@type":"Person","name":"simeyla"},"datePublished":"2019-03-04T05:00:40.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/29089/angular/issues/29089"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:06820462-9002-2f59-2c01-5c42cbc254de |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CA34:12B146:638570:8CBD4E:6A62400B |
| html-safe-nonce | 1604527a2b66d7b42fe0426b2bdecb00f078c675d872a84d74a9ba3c812c71f1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQTM0OjEyQjE0Njo2Mzg1NzA6OENCRDRFOjZBNjI0MDBCIiwidmlzaXRvcl9pZCI6IjUzMTE0MDc3MTY4MjA5OTIwMTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e42409915fe7c812ec239ed07660c2d8f6baa2138011f90705450ad0f75bca51 |
| hovercard-subject-tag | issue:416631206 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/29089/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9a33204d434e3f6cdf0cf590a653f6fabe22382bae9dc8919715e8f5c76066bf/angular/angular/issues/29089 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9a33204d434e3f6cdf0cf590a653f6fabe22382bae9dc8919715e8f5c76066bf/angular/angular/issues/29089 |
| og:image:alt | 🚀 feature request Relevant Package @angular router Description I see that in the docs it is recommended to do the following to cancel a navigation event in a Resolve |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | simeyla |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5d6ba65d73ecc4e3394fe318d2b2f98e6f8eed4878b5421b938e20d30bde267b |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2dadc56fd5989b76a8ae7304e3aa56d0b485e5dc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width