Executing resolvers one after the other in Angular 2+

后端 未结 4 1566
孤城傲影
孤城傲影 2021-02-04 03:50

Given the following route:

path: \'\',
component: MyComponent,
resolve: {
    foo: FooResolver,
    bar: BarResolver
}

Is there any way of tell

4条回答
  •  一整个雨季
    2021-02-04 04:37

    I tackled this scenario using combineLatest in a single resolver. You can do this:

    @Injectable({providedIn: 'root'})
    export class FooBarResolver implements Resolve {
      constructor(
        private readonly fooResolver: FooResolver,
        private readonly barResolver: BarResolver  
      ) {}
    
      resolve() {
        return combineLatest(
          this.fooResolver.resolve(), 
          this.barResolver.resolve()
        ).pipe(map(([users, posts]) => ({users, posts})))
      }
    }
    

提交回复
热议问题