Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Angular 运行一个observable,当另一个发出时,返回两个结果_Angular_Typescript_Rxjs - Fatal编程技术网

Angular 运行一个observable,当另一个发出时,返回两个结果

Angular 运行一个observable,当另一个发出时,返回两个结果,angular,typescript,rxjs,Angular,Typescript,Rxjs,我有以下代码: dialog.afterClosed() .pipe( filter((result) => result), mergeMap((result) => this.unitService.importPack(result.file)) ) .subscribe((result) => { console.log(result); // I need the result of

我有以下代码:

dialog.afterClosed()
      .pipe(
        filter((result) => result),
        mergeMap((result) => this.unitService.importPack(result.file))
      )
      .subscribe((result) => {
        console.log(result); // I need the result of both, not only the mergeMap result
        this.updateStateAfterUpload(result.file, 'imported');
      });
这里的问题是,我订阅中的
结果
是在我的
mergeMap
操作符中返回的可观察结果,但我实际上需要同时使用这两种结果-从我的
afterClosed()
可观察返回的结果和由
mergeMap
操作符返回的结果

如何在订阅中返回这两个观察结果

dialog.afterClosed().pipe(
  mergeMap(result1 => this.unitService.importPack(result.file).pipe(
    map(result2 => ({ result1, result2 }))
  ))
).subscribe(({ result1, result2 }) => { ... });
映射第二个请求的结果以返回所需的结果。这里,我返回类型为
{result1:any,result2:any}


映射第二个请求的结果以返回所需的结果。在这里,我返回一个类型为
{result1:any,result2:any}

的对象,您是否查看过@UmutEsen forkjoin发出闭合观测值的最后一个值。在本例中,第二个可观察对象使用第一个的结果。使用mergeAll@NarendraSinghRathore
在许多情况下,您可以使用mergeMap作为单个操作符
你看过@UmutEsen forkjoin发出闭合观测值的最后一个值了吗。在本例中,第二个可观察对象使用第一个的结果。使用mergeAll@NarendraSinghRathore
在许多情况下,您可以使用mergeMap作为单个操作符看起来不错!但是在回答的
map
操作符中使用语法时出现了一个错误(
TS2695:逗号操作符的左侧未使用并且没有副作用)。事实上,我忘记了括号:
map(result2=>({result1,result2}))
看起来不错!但是在回答的
map
操作符中使用语法时出现了一个错误(
TS2695:逗号操作符的左侧未使用并且没有副作用)。事实上,我忘记了括号:
map(result2=>({result1,result2}))