Skip to content

Router guards: need a way to determine which parameter has changed #14283

Closed

Description

@dawidgarus

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

Current behavior
Currently, when route parameters change, resolvers are executed, but there is no way of knowing which parameter has changed.
Lets say we have a route:

{
  path: 'details/:firstId/:secondId',
  component: Details,
  resolve: { first: FirstResolver, second: SecondResolver }
}

When any of firstId or secondId changes, both FirstResolver and SecondResolver are executed.

Expected behavior
Firstly, we need a way to determine which parameter has changed.
Secondly, we need a way to return previously resolved value if the relevant parameter hasn't changed.
This can be archived with simple (from the user's point of view) change:
Route config should expect array of providers in canActivate, canDeactive, canActivateChild and canLoad properties and map of string => provider as resolve property.
This way, we don't need to provide guards at module level. The guard instance will be created when user wants to activate route, before the component instance is created and destroyed when user deactivates route.
Since guards will no longer be singletons (they will be unique per component instance), it would be possible to store in them some data, unique for the component, for example:

@Injectable()
export class DetailsResolver implements Resolve<any> {
  private lastId: string = null;
  private lastValue: any;
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<any>|Promise<any>|any {
    if (this.lastId === null || this.lastId === route.params['id']) {
      // actual resolving
    } else {
      return this.lastValue
    }
  }
}

Minimal reproduction of the problem with instructions
http://plnkr.co/edit/Krok4FpJoTmm8fmdqUsi

What is the motivation / use case for changing the behavior?
To stop resolver from performing unnecessary ajax calls.

Please tell us about your environment:

  • Angular version: 2.4.6
  • Browser: all
  • Language: all

Activity

  1. added
    feature
    on Dec 19, 2017
  2. added this to the Backlog milestone on Jan 23, 2018
  3. 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.

  4. angular-robot commented on Jun 25, 2021

    @angular-robot
    Contributor

    Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

    We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

    You can find more details about the feature request process in our documentation.

  5. angular-automatic-lock-bot commented on Aug 23, 2021

    @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.

  6. locked and limited conversation to collaborators on Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routerfeaturefreq2: medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions