Skip to content

NavigationExtras when returning URLTree from guard #27148

@Airblader

Description

@Airblader

🚀 feature request

Relevant Package

This feature request is for @angular/router

Description

In #26521 (Angular 7.1.0), a feature was introduced that allows returning a URLTree from guards in order to initiate a redirect. However, this doesn't seem to allow using NavigationExtras such as skipLocationChange.

Since the change in #26521 wasn't just syntax sugar, but is relevant to have predictable behavior when dealing with multiple guards, it should be considered to somehow allow using these options when returning a URLTree as well.

Describe the solution you'd like

I don't have a really good proposal at this moment apart from next to returning URLTree also returning some kind of

// TODO: Find a better name?
interface NavigationOptions {
  urlTree: URLTree;
  navigationExtras: NavigationExtras;
}

such that I could use

class AwesomeGuard implements CanActivate {
  constructor(private router: Router) {}

  canActivate(): NavigationOptions | Observable<NavigationOptions> {
    return {
      urlTree: this.router.parseUrl("…"),
      navigationExtras: {
        skipLocationChange: true,
      },
    };
  }
}

I guess the exact typing of the guard would have to be

// Just a placeholder name
type GuardReturnType = boolean | URLTree | NavigationOptions;

interface CanActivate {
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): 
    GuardReturnType 
    | Promise<GuardReturnType> 
    | Observable<GuardReturnType>
}

Admittedly, the return type of this function is growing increasingly complex with this.

Describe alternatives you've considered

Possible options that I don't think would make good solutions would be

  • Returning an array [URLTree, NavigationExtras] (this just feels unclean)
  • Extending URLTree with the navigationExtras and extend parseUrl to take them as well (this muddies the entire API for a change in a specific location)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routerfeatureIssue that requests a new featurefeature: under considerationFeature request for which voting has completed and the request is now under considerationstate: has PR

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions