feat(router): add method to convert an Url to RouterStateSnapshot#30411
feat(router): add method to convert an Url to RouterStateSnapshot#30411yelhouti wants to merge 1 commit intoangular:masterfrom
Conversation
convert any url to a routeStateSnapshot (if they exist). This allows to check if user can access link before displaying it rerun CI
| return urlTree; | ||
| } | ||
|
|
||
| /** Converts given Url to RouterStateSnapshot */ |
There was a problem hiding this comment.
| /** Converts given Url to RouterStateSnapshot */ | |
| /** Converts given URL to `RouterStateSnapshot` */ |
There was a problem hiding this comment.
This could also use some additional documentation. It should be noted that if you pass a URL to a lazy-loaded path, that module will actually get loaded by applyRedirects. We might want to think about these kinds of unintended consequences more before adding this feature.
| let resolve: any = null; | ||
| let reject: any = null; | ||
|
|
||
| const promise = new Promise<boolean>((res, rej) => { | ||
| resolve = res; | ||
| reject = rej; | ||
| }); | ||
| const transition: NavigationTransition = { | ||
| ...this.getTransition(), | ||
| id: 1, | ||
| source: 'imperative', | ||
| restoredState: null, | ||
| currentUrlTree: this.currentUrlTree, | ||
| currentRawUrl: this.rawUrlTree, | ||
| rawUrl: mergedTree, resolve, reject, promise, | ||
| currentSnapshot: this.routerState.snapshot, | ||
| currentRouterState: this.routerState, | ||
| extractedUrl: this.urlHandlingStrategy.extract(mergedTree) | ||
| }; | ||
| const moduleInjector = this.ngModule.injector; | ||
| return of (transition) |
There was a problem hiding this comment.
| let resolve: any = null; | |
| let reject: any = null; | |
| const promise = new Promise<boolean>((res, rej) => { | |
| resolve = res; | |
| reject = rej; | |
| }); | |
| const transition: NavigationTransition = { | |
| ...this.getTransition(), | |
| id: 1, | |
| source: 'imperative', | |
| restoredState: null, | |
| currentUrlTree: this.currentUrlTree, | |
| currentRawUrl: this.rawUrlTree, | |
| rawUrl: mergedTree, resolve, reject, promise, | |
| currentSnapshot: this.routerState.snapshot, | |
| currentRouterState: this.routerState, | |
| extractedUrl: this.urlHandlingStrategy.extract(mergedTree) | |
| }; | |
| const moduleInjector = this.ngModule.injector; | |
| return of (transition) | |
| const moduleInjector = this.ngModule.injector; | |
| return of ({extractedUrl: this.urlHandlingStrategy.extract(mergedTree)} as NavigationTransition) |
I think this can be simplified quite a bit. The applyRedirects only uses extractedUrl from the transition and recognize only uses urlAfterRedirects which is set by applyRedirects.
|
|
||
| class ComponentA {} | ||
| class ComponentB {} | ||
| class ComponentC {} |
There was a problem hiding this comment.
Could you place these inside the describe instead?
| describe('urlToRouteStateSnapshot', () => { | ||
| beforeEach(() => { TestBed.configureTestingModule({imports: [RouterTestingModule]}); }); | ||
|
|
||
| it('should return snapshot or exception', inject([Router], async(r: Router) => { |
There was a problem hiding this comment.
It would also probably be a good idea to cover the following scenarios as well:
- loadChildren
- canActivate
- rejecting
- returning
UrlTree
- redirectTo
|
Closing as duplicate of #15826 |
|
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. |
convert any Url to a routeStateSnapshot (if they exist).
This allows to check if user can access link before displaying it
fixes #15750
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Enable to retrieve RouterStateSnapshot from URL
Issue Number: 15750
What is the new behavior?
Router allows to retrieve RouterStateSnapshot from Url
Does this PR introduce a breaking change?
Other information