Skip to content

Add canUse guard #14515

Closed
Closed

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.

Activity

  1. DzmitryShylovich commented on Feb 15, 2017

    @DzmitryShylovich
    Contributor

    Sounds similar to #12499

  2. added
    featureLabel used to distinguish feature request from other issues
    on Feb 15, 2017
  3. vicb commented on Feb 15, 2017

    @vicb
    Contributor

    Sounds good, what about when: instead of canUse: ?

  4. alexbyk commented on Mar 16, 2017

    @alexbyk
    Contributor

    Any progress on this feature?

  5. itrethan commented on Mar 31, 2017

    @itrethan

    LGTM, Approval

  6. added this to the Backlog milestone on Jan 23, 2018
  7. angular-robot commented on Jun 4, 2021

    @angular-robot
    Contributor

    Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

    Find more details about Angular's feature request process in our documentation.

  8. angular-robot commented on Jun 25, 2021

    @angular-robot
  9. removed this from the Backlog milestone on Oct 27, 2021
  10. 19 remaining items

  11. atscott commented on May 3, 2022

    @atscott
    Contributor

    @dylhunn I've considered sending the PR out as blocked by #45023 due to not wanting to have too many in-flight reviews. Our bigger projects have held up some of these smaller tasks. I think this feature may be more straightforward than #45023 so I'll try to get it out for 14.1

  12. added a commit that references this issue on Jun 10, 2022
    a99727f
  13. angular-automatic-lock-bot commented on Jul 14, 2022

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  14. locked and limited conversation to collaborators on Jul 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: routerfeatureLabel used to distinguish feature request from other issuesfeature: 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