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}))