Skip to content

Add canUse guard #14515

@vsavkin

Description

@vsavkin

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Currently we have two main types of guards:

  • CanLoad: decides if we can load a module (used with lazy loading)
  • CanActivate and friends. It decides if we can activate/deactivate a route.

So we always decide where we want to navigate first ("recognize") and create a new router state snapshot. And only then we run guards to check if the navigation should be allowed.

This doesn't handle one very important use case where we want to decide where to navigate based on some data (e.g., who the user is).

I suggest to add a new guard that allows us to do that.

[
  {path: 'home', component: AdminHomePage, canUse: [IsAdmin]},
  {path: 'home', component: SimpleHomePage}
]

Here, navigating to '/home' will render `AdminHomePage' if the user is an admin and will render 'SimpleHomePage' otherwise. Note that the url will remain '/home'.

Implementation Notes

  • This should be handled in both applyRedirects and recognize.

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions