Resolver does not resolve if another data stream is added

后端 未结 2 332
孤街浪徒
孤街浪徒 2021-01-22 17:07

I am trying to use a resolver in order to retrieve data depending on the given parameters the route holds.

Unfortunately, the moment I add another data stream that my da

相关标签:
2条回答
  • 2021-01-22 17:47

    The answer to my problem is rather simple and had nothing to do with subscribing to the resolved observables, as I already did that within the (target)component.

    In order for a resolver to finish, all the streams it depends on need to complete. If you happen to use a hot observable it is required to use another operator like take so that the stream completes at that location.

    So, all the code remains the same, except that I changed the resolver to:

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Foo> {
      return this._secondService
        .observable()
        .pipe(
          take(1),
          switchMap((bar) => this._myService.get(bar)),
      );
    }
    

    @eduPeeth: Thank you for your answer/suggestions, unfortunately, it was a far more minor issue.

    0 讨论(0)
  • 2021-01-22 17:53

    Observable only get executed when you call Observable.subscribe() explicitly . I don't see anywhere in your code where you are subscribing to your Observables.

    Note : Concept of resolve is related to Promise not with Observable.

    Try :

    export class MyResolver {
      constructor(_secondService: SecondService, _myService: MyService) {}
    
      resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Foo> {
        // Does not work - Simply does not resolve
         return this._secondService
           .observable()
           .pipe(
             take(1),
             switchMap((bar) => this._myService.get(bar)),
           }     
           );
    
        // WORKS
        return of(new Foobar('sampleData')).pipe(
             take(1),
             switchMap((bar) => this._myService.get(bar)),          
         });
      }
    }
    

    Subscribe where you need the result.

    0 讨论(0)
提交回复
热议问题