我正在尝试合并三个可观察对象,当内部可观察对象没有任何数据时,mergeMap 不会返回任何数据。即使内部可观察对象之一为空,我也希望能够继续该过程。我该如何处理这种情况?这是我的代码:
ngOnInit() {
this.accountStatusSub = this.accountService.accountNumberChange$.subscribe(
accNumber =>
{this.accountNumber = accNumber;
var obs = this.accountService.getAccountDetails(this.accountNumber)
.pipe(mergeMap(accountData =>
this.accountService.getBill(accountData.account[0].accountNumber)
.pipe(mergeMap(billData =>
this.accountService.getPayment(accountData.account[0].accountNumber)
.pipe(map(paymentData => ({
address1: accountData.account[0].address1,
address2: accountData.account[0].address2,
city: accountData.account[0].city,
state: accountData.account[0].state,
zip: accountData.account[0].zip,
amountDue: billData.bill[0].amountDue,
dueDate: billData.bill[0].dueDate,
lastPaymentAmount: paymentData.payment[0].paymentAmount,
lastPaymentDate: paymentData.payment[0].paymentDate
})
))
))
))
obs.subscribe(combinedAccountData => {
console.log('MergeMap:', combinedAccountData)
})
})
}
当 billData 或 paymentData 为空时,combinedAccountData 为空。有没有更好的方法来编写上面的代码?我是 Angular 和 rxjs 的新手。谢谢。