Skip to content

Router nested resolve #18092

Closed
Closed

Description

@dragospeta

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Route resolve provider functions that are making a request on server are loading and making the request at the same time. It is a normal behavior but there is a situation when one resolve result depends on another, more like promises.

Expected behavior

In Angular.js, using the ui-router, i could inject into a route resolve function an already resolved one. I will put an example code here:
a
As you can see i can use the X result in Y.

The thing is that in order to do this in Angular i have to use the ngOnInit function, get the resolved function from a parent route or to make a resolve provider that makes a server request and then does the other. These solutions make the code look bad and i would like to know if there is a solution for this.

So, from my experience and knowledge, in Angular i can't inject the result of a previous route resolve like in Angular.js ui-router because i'm forced to provide a class implementing the Resolve Interface that has a function where i can't access previous resolved parameters.

To make the question even more clear. i will add 2 more pics with an example of Angular code:
a2

a3

What is the motivation / use case for changing the behavior?

There are rare cases where one route resolve provider depends on another.

Activity

  1. noamichael commented on Sep 8, 2017

    @noamichael

    Could you use one resolver with a promise chain?

    @Injectable
    export class GetBothItems implements Resolve<{x: any, y: any}>{
        constructor(
            private serviceA: ServiceA,
            private serviceB: ServiceB
        ){}
        
        resolve( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ){
            return this.serviceA.getStuff().then(x => {
                return this.serviceB.getOtherStuff(x).then(y => {
                    return {x, y};
                 });
            });
    
        }
    }
    
  2. dragospeta commented on Sep 8, 2017

    @dragospeta
    Author

    Thanks for your answer @noamichael . In the end i had to choose for this specific problem between your answer, ngOnInit and Guards. I requested this feature because it would be useful to separate requests in order to have a better organised code.

  3. xaralis commented on Dec 5, 2017

    @xaralis

    This is someting that should have real priority. I wouldn't say these cases are rare, they are actually quite common when someone is implementing anything more serious than merely just example todo lists.

    It would be nice if at least parent resolvers were run before child guards. If this doesn't work, working with real-world data flows and more complicated tasks quickly becomes big pain in the ass.

  4. jasonaden commented on Dec 7, 2017

    @jasonaden
    Contributor

    This is not something we are looking at implementing at this time. I think we got used to this behavior with ui-router in AngularJS, but that worked due to a different type of dependency injection.

    A better pattern would be to use something other than Resolve to get your data. Use a service that manages the dependencies. This will be much more testable.

    Additionally, we would like to look at passing Observables in through something like a Resolve, but instead of the current implementation you would be able to combine them together and they wouldn't need to be completed like Resolve currently does.

  5. angular-automatic-lock-bot commented on Sep 12, 2019

    @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 Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions