简体   繁体   English

如何在 Angular 中的第一个服务响应成功时进行顺序服务调用

[英]How to make sequential service call on success of first service response in Angular

I need to make multiple service call in angular one after other.我需要一个接一个地进行多次服务调用。 need to pass the first service call respose as input to another service.需要将第一个服务调用 respose 作为输入传递给另一个服务。

Here is my component:这是我的组件:

 Demo(): any {
        if (fileToUpload) {
          this._voiceboxService.upload(fileToUpload)
          .subscribe((res: any) => {
            this.text=res.prediction
              console.log(res);
          });
      }
      else
          console.log("FileToUpload was null or undefined.");
    }
}

Here is my Service: i need to call all three service on success of one service and need to pass first service resposnse as input for next service这是我的服务:我需要在一个服务成功时调用所有三个服务,并且需要传递第一个服务响应作为下一个服务的输入

          upload(fileToUpload: any) {
            let input = new FormData();
            input.append("file", fileToUpload);
           return this.http.post<any>('https://localhost:5001/', input)
         
          language(data: any) {
           return this.http.post<any>('https://localhost:5002', data)
          }
    
        getDetails(data: any) {
           return this.http.post<any>('https://localhost:5003', data)
          }

Use mergeMap.使用合并映射。 I assume you want to do this in your component:我假设您想在您的组件中执行此操作:

   this._voiceboxService.upload(fileToUpload).pipe(mergeMap(upload => 
       this._voiceboxService.language(upload)
          .pipe(mergeMap(language => this._voiceboxService.getDetails(language))
   ))).subscribe((res: any) => {
        this.text=res.prediction
        console.log(res);
   });

You can use map in the end organize your final value result.您可以使用 map 最终组织您的最终值结果。

You could use any of the RxJS higher order mapping operators like switchMap to map from one observable to another.您可以使用任何 RxJS 高阶映射运算符(如switchMap来从一个可观察switchMap映射到另一个可观察对象。 You could find differences between different mapping operators here .您可以在此处找到不同映射运算符之间的差异。

Service服务

upload(fileToUpload: any) {
  let input = new FormData();
  input.append("file", fileToUpload);
  return this.http.post<any>('https://localhost:5001/', input).pipe(
    switchMap(res => this.language(res)),  // <-- `res` = response from previous request
    switchMap(res => this.getDetails(res)) // <-- `res` = response from `this.language()`
  );
}

language(data: any) {
  return this.http.post<any>('https://localhost:5002', data)
}

getDetails(data: any) {
  return this.http.post<any>('https://localhost:5003', data)
}

Component成分

Demo(): any {
  if (fileToUpload) {
    this._voiceboxService.upload(fileToUpload).subscribe({
      next: (res: any) => {                // <-- `res` = response from `getDetails()`
        this.text = res.prediction
        console.log(res);
      },
      error: (error: any) => {
        // handle errors
      }
    });
  } else {
    console.log("FileToUpload was null or undefined.");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM