Angular 4 - How can get value synchronously from an http request in another http request?

后端 未结 2 1407
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 17:01

I\'m using Angular 4 for my client application.

The getCustomer() function in customer.service calls an API to get data. This function is Observable.

This

相关标签:
2条回答
  • 2021-01-03 17:39

    I'm using flatMap and it works now. Thank you very much for your suggestions @AJT_82

    0 讨论(0)
  • 2021-01-03 17:40

    Off the top of my head, in your customer.service.ts you can chain method calls with something like this:

    getCustomer(id: string): Observable<CustomerResource> {
      this.authService.getAccessToken()
        .map((accessToken) => { 
          let headers = addAccessTokenToHeader(accessToken);
    
          let url = `${this.customersUrl}/${id}`;
          return this.http.get(url, { headers: headers })
            // rest of the code
          }); 
    

    If it doesn't work, try replacing .map with .switchMap.

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