Angular 如何连接两个响应,按顺序调用。第二个http调用取决于第一个http调用返回的值?

Angular 如何连接两个响应,按顺序调用。第二个http调用取决于第一个http调用返回的值?,angular,typescript,rxjs,Angular,Typescript,Rxjs,现在我有了那个代码,并尝试从两个http调用返回一致的值 defer(() => { return this.service.save1(data) .pipe( concatMap((result) => { if (result) { retu

现在我有了那个代码,并尝试从两个http调用返回一致的值

defer(() => {
                return this.service.save1(data)
                    .pipe(
                        concatMap((result) => {
                            if (result) {
                                return of({
                                    ...result,
                                    c: this.service.save2(result.id , this.obj).subscribe()    
                                })
                            }
                            return of({});
                        }),
                        catchError(err => throwError(err)),
            }).subscribe((result) => {
                if (result) {
                    console.log(result);
                }
            },
在cosnole.log(result)中,现在我们有了整个对象结果+c:作为可观察的
我想返回第一次调用返回的所有字段,c:作为对象数组

可以通过管道传输observable
c
的响应来格式化响应。无需订阅该可观测值,它将自动订阅concatMap的值

 defer(() => {
    return this.service.save1(data)
        .pipe(
            concatMap((result) => {
                if (result) {
                    return this.service.save2(result.id , this.obj).pipe(
                            map((data) => [...data, result]) // all the data is 
                                                             // available here, format 
                                                             // as you would like
                }
                return of({});
            }),
            catchError(err => throwError(err)),
}).subscribe((result) => {
    if (result) {
        console.log(result);
    }
},

您可以通过管道化可观察
c
的响应来格式化响应。无需订阅该可观测值,它将自动订阅concatMap的值

 defer(() => {
    return this.service.save1(data)
        .pipe(
            concatMap((result) => {
                if (result) {
                    return this.service.save2(result.id , this.obj).pipe(
                            map((data) => [...data, result]) // all the data is 
                                                             // available here, format 
                                                             // as you would like
                }
                return of({});
            }),
            catchError(err => throwError(err)),
}).subscribe((result) => {
    if (result) {
        console.log(result);
    }
},

您不必在运营商内部订阅。相反,您可以直接返回可观测值,并在需要结果的地方订阅

  • 使用
    filter
    pipe忽略来自第一个请求的未定义响应。这样就不需要
    if
    条件下的
    concatMap
    和订阅
  • 通过管道将
    映射
    连接到第二个可观察对象,以合并两个结果
  • 试试下面的方法

    返回此.service.save1(数据).pipe(
    过滤器((结果)=>(!!结果))//
    this.service.save2(result.id,this.obj).pipe(
    地图((c)=>({
    …结果,
    c:c
    }))
    )
    ),
    
    catchError(err=>throwError(err))//您不必在操作符内部订阅。相反,您可以直接返回可观测值,并在需要结果的地方订阅

  • 使用
    filter
    pipe忽略来自第一个请求的未定义响应。这样就不需要
    if
    条件下的
    concatMap
    和订阅
  • 通过管道将
    映射
    连接到第二个可观察对象,以合并两个结果
  • 试试下面的方法

    返回此.service.save1(数据).pipe(
    过滤器((结果)=>(!!结果))//
    this.service.save2(result.id,this.obj).pipe(
    地图((c)=>({
    …结果,
    c:c
    }))
    )
    ),
    
    catchError(err=>throwError(err))//谢谢这是我所期望的谢谢这是我所期望的