【问题标题】:How to send Multiple Http request sequentially in Angular如何在Angular中按顺序发送多个Http请求
【发布时间】:2019-04-29 10:48:01
【问题描述】:

我已经编写了以下代码,每次在发布请求发生之前调用 API, 第一个 API 被调用,第二个没有被调用

  public post(postUrl: string, model: any): Observable<any> {
    return this.validateTokenStatus().pipe(map(response => {
        console.log('response', response);
        // if (response) {
        console.log('response2', response);
        const url = `${environment.webApiUrl}/${postUrl}`;
        this.spinnerService.start();
        console.log('response21', response);
        return this._http.post(url, model).pipe(map((res: any) => {
            console.log('response11', response);
            this.spinnerService.stop();
            return res;
        },
        error => {
            console.log('error');
            return error;
        }));
        // } else {
       // console.log('response3', response);
        // return true;
        // }
    }));
}

【问题讨论】:

标签: angular rxjs angular6 angular7


【解决方案1】:

当您想依次执行多个异步操作时,通常需要使用 mergeMap、switchMap 或 concatMap 之一。在这种情况下,这样的事情可能会起作用:

return this.validateTokenStatus()
  .pipe(
    switchMap(response => {
        const url = `${environment.webApiUrl}/${postUrl}`;
        this.spinnerService.start();
        return this._http.post(url, model);
    }),
    map((res: any) => {
        this.spinnerService.stop();
        return res;
    })
  );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    相关资源
    最近更新 更多