feat(router): allow CanLoad guard to return UrlTree (#36610)

A CanLoad guard returning UrlTree cancels current navigation and redirects.
This matches the behavior available to `CanActivate` guards added in #26521.

Note that this does not affect preloading. A `CanLoad` guard blocks any
preloading. That is, any route with a `CanLoad` guard is not preloaded
and the guards are not executed as part of preloading.

fixes #28306

PR Close #36610
This commit is contained in:
Andrew Scott
2020-04-13 10:50:44 -07:00
committed by Andrew Kushnir
parent cae2a893f2
commit 00e6cb1d62
4 changed files with 150 additions and 66 deletions

View File

@ -339,6 +339,10 @@ export interface Resolve<T> {
* @description
*
* Interface that a class can implement to be a guard deciding if children can be loaded.
* If all guards return `true`, navigation will continue. If any guard returns `false`,
* navigation will be cancelled. If any guard returns a `UrlTree`, current navigation will
* be cancelled and a new navigation will be kicked off to the `UrlTree` returned from the
* guard.
*
* ```
* class UserToken {}
@ -400,8 +404,9 @@ export interface Resolve<T> {
* @publicApi
*/
export interface CanLoad {
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean>|Promise<boolean>|boolean;
canLoad(route: Route, segments: UrlSegment[]):
Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree;
}
export type CanLoadFn = (route: Route, segments: UrlSegment[]) =>
Observable<boolean>|Promise<boolean>|boolean;
Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree;