Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于第一个可观察到的数据调用其他服务并等待服务给出结果_Javascript_Angular_Typescript_Rxjs - Fatal编程技术网

Javascript 基于第一个可观察到的数据调用其他服务并等待服务给出结果

Javascript 基于第一个可观察到的数据调用其他服务并等待服务给出结果,javascript,angular,typescript,rxjs,Javascript,Angular,Typescript,Rxjs,我确实遇到了一个棘手的情况,我确实有以下情况 根据我的第一个观察结果,我计划呼叫服务第一服务1 同时,我确实需要调用service2,并需要等待service1和service2返回数据 我在考虑使用switchmap从第一个可观察对象获取数据,然后对其他2个服务使用CombineTest,但没有得到正确的语法 我在下面试过 getData(){ //装载开始 this.query.currentData$ .烟斗( 开关映射((代码)=>{ //要使用最新版本吗 //需要调用2个服务并等待获

我确实遇到了一个棘手的情况,我确实有以下情况

根据我的第一个观察结果,我计划呼叫服务第一服务1

同时,我确实需要调用service2,并需要等待service1和service2返回数据

我在考虑使用switchmap从第一个可观察对象获取数据,然后对其他2个服务使用CombineTest,但没有得到正确的语法

我在下面试过

getData(){
//装载开始
this.query.currentData$
.烟斗(
开关映射((代码)=>{
//要使用最新版本吗
//需要调用2个服务并等待获取数据
//一旦两个数据都出现,请加载子组件
//为服务添加finalize
}),
catchError((错误:HttpErrorResponse)=>{
//装卸站
返回投掷器(错误);
}),
完成(()=>
//装卸站
),
takeUntil(此为.\u ngUnsubscribe$),
)
.subscribe(()=>{});

}
您可以使用
forkJoin
等待两个或多个服务响应

getData() {
    //loading start
    this.query.currentData$
        .pipe(
            switchMap((code) => {
                return forkJoin([this.service1(code), this.service2(code)])
            }),
            catchError((error: HttpErrorResponse) => {
                //loading stop
                return throwError(error);
            }),
            finalize(() =>
                //loading stop
            ),
            takeUntil(this._ngUnsubscribe$),
        )
        .subscribe(() => {});

}

您可以使用
forkJoin
等待两个或多个服务响应

getData() {
    //loading start
    this.query.currentData$
        .pipe(
            switchMap((code) => {
                return forkJoin([this.service1(code), this.service2(code)])
            }),
            catchError((error: HttpErrorResponse) => {
                //loading stop
                return throwError(error);
            }),
            finalize(() =>
                //loading stop
            ),
            takeUntil(this._ngUnsubscribe$),
        )
        .subscribe(() => {});

}