如何在angular 6中调用多个API并订阅?

如何在angular 6中调用多个API并订阅?,angular,request,observable,Angular,Request,Observable,在我的应用程序中,要调用所有POST请求,我使用了服务 当我从服务器获得一个特定的代码(E.x:401)时,我调用一个API来获取新的令牌 在收到另一个令牌之前,如果有来自的任何其他API调用,我将所有这些请求存储在一个数组中。它可能是n请求。现在假设在调用newToken API的过程中有3个API调用 一旦我得到了一个新的令牌,我就必须在所有后续的API中传递它。现在,我必须执行所有挂起的API请求,并为它们各自的调用提供数据 代码示例: api.service.ts POST(URL ,

在我的应用程序中,要调用所有
POST
请求,我使用了
服务

当我从服务器获得一个特定的代码(E.x:401)时,我调用一个API来获取新的令牌

在收到另一个令牌之前,如果有来自的任何其他API调用,我将所有这些请求存储在一个数组中。它可能是n请求。现在假设在调用newToken API的过程中有3个API调用

一旦我得到了一个新的令牌,我就必须在所有后续的API中传递它。现在,我必须执行所有挂起的API请求,并为它们各自的调用提供数据

代码示例:

api.service.ts

POST(URL , param){
   return new Observable<any>(observer => {

  let headers = new HttpHeaders({
    'Content-Type': 'Content-Type': 'application/json'
  });

  let options = {
    headers: headers
  };

  this.http.post(URL, param, options)
        .subscribe(data => {
          var apiRes: any = data;                                       
          this.inValidSession();
          observer.next();
          observer.complete();              
  }
  ......

  //  For execute pending request I have set this 

  for (let i = 0; i < this.queue.length; i++) {                          
        this.REPOST(this.queue[i].param, this.queue[i].url).subscribe((queueResponse) => {             
          observer.next(queueResponse);
          observer.complete();
          this.queue.shift();                      
       });
   }
}
ngOnInit(){
    this.getUserData();        
    this.getProductData();
}

getUserData(){
   this.apiService.post({},'/apiName').subscribe((response) => {
       console.log(response);
   })
}

getProductData(){
   this.apiService.post({},'/apiName2').subscribe((response) => {
       console.log(response);
   })
}
问题是,当我执行所有挂起的API时,我会在控制台中获取数据。但不从服务文件订阅到相应的
.ts
文件功能

注意:我只在一个函数中获得订阅数据,而不是每个函数。换句话说,我在
getProductData()
函数中获得两个API res。我不知道为什么

如果有人有解决办法,请帮助我

您可以使用

forkJoin()
要同时处理多个调用,您可以调用多个
请求
,订阅后您将获得一个
响应数组

乙二醇

其中,service1和service2是具有函数ccall1和call2的服务,它们具有
返回
类型
可观察


您可以从
rxjs

import { zip } from "rxjs";

myFun() {    
   zip(service.api1, service.api2)
       .subscribe(([response1, response2]) => {
          console.log(response1);
          console.log(response2);
    })
}

简易方法

使用forkjoin()

public sample():可观察
{
让call1=this.http.get(this.\u url+'/v1/sample1')
让call2=this.http.get(this.\u url+'/v1/sample2')
让call3=this.http.get(this.\u url+'/v1/sample3')
返回forkJoin([call1,call2,call3]);
}

forkJoin()
将有助于一次进行多个调用time@JavascriptLover-SKT我尝试过使用它,我在控制台中以数组的形式获得了数据。但是我的
订阅
不起作用。这就是问题所在。当我从
ts
直接调用时,它也会很有用。但是我使用了服务来重用代码。我已经尝试使用
forkJoin
,但它在一次内返回所有数据。然后我无法将值返回到
ts
。我的意思是,当我使用
forkJoin
时,我的
subscribe
不起作用。你可以直接返回forkJoin(Service1.call1,Service2.call2),或者如果你正在订阅它,那么你应该将结果作为一个可观察对象返回:forkJoin(Service1.call1,Service2.call2)。subscribe(()=>//返回包含结果的新可观察对象)
import { zip } from "rxjs";

myFun() {    
   zip(service.api1, service.api2)
       .subscribe(([response1, response2]) => {
          console.log(response1);
          console.log(response2);
    })
}
public sample(): Observable<any[]>
{

let call1=this.http.get(this._url+'/v1/sample1')

let call2=this.http.get(this._url+'/v1/sample2')

let call3=this.http.get(this._url+'/v1/sample3')

return forkJoin([call1, call2,call3]);

}