Angular RxJS - 只有当订阅另一个 Observable 给出特定结果时才订阅 Observable
Angular RxJS - Subscribe to Observable only IF subscribing to another Observable gave a certain result
我需要某种特殊的 RxJS 语法(如果它适用于这种情况):
this.electronicInvoiceService.getElectronicInvoice(this.invoiceId)
.subscribe(and here I can get .isApproved, for example:
(data) => data.isApproved
and what I want is IF data.isApproved is false only then
I want to subscribe to this
next observable I wrote below this);
仅当 isApprove 为 false 时才执行下一次订阅
this.electronicInvoiceStagingService.getElectronicInvoice(this.invoiceId).subscribe();
我已经尝试过的:我尝试在我的 ts 文件和内部订阅中添加一些变量我做了 this.myVariableName == data.isApproved 但这没有用我认为因为订阅需要一些时间这是错误的逻辑。
[解决方案]:下面的评论解决了它,我真傻我没有马上使用它(:解决方案>为什么简单的 if 语句不够?(数据)=> {if(!data.isApproved) this.electronicInvoiceService.getElectronicInvoice(this.invoiceId).subscribe();}
其他订阅中的订阅不被视为 RxJs 中的最佳实践。
我认为更惯用的解决方案可能如下
this.electronicInvoiceService.getElectronicInvoice(this.invoiceId)
// here you start a pipe, i.e. a series of operations that transform the source stream
.pipe(
// concatMap is the first operator which basically says: wait for upstream
// to notify and then take the data notified and return a new Observable
concatMap( data => {
// here you put your if logic
if (!data.isApproved) {
// return the other Observable
return this.electronicInvoiceStagingService.getElectronicInvoice(this.invoiceId)
}
// otherwise return EMPY, which is an Observable that does nothing and just
// completes immediately
return EMPTY
})
)
// eventually you just subscribe to the one and only one stream you have created
// transforming, with the pipe, the original source stream
.subscribe(dataFromStagingService => {
// do anything you need to do with the data returned by the staging service call
})
我需要某种特殊的 RxJS 语法(如果它适用于这种情况):
this.electronicInvoiceService.getElectronicInvoice(this.invoiceId)
.subscribe(and here I can get .isApproved, for example:
(data) => data.isApproved
and what I want is IF data.isApproved is false only then
I want to subscribe to this
next observable I wrote below this);
仅当 isApprove 为 false 时才执行下一次订阅
this.electronicInvoiceStagingService.getElectronicInvoice(this.invoiceId).subscribe();
我已经尝试过的:我尝试在我的 ts 文件和内部订阅中添加一些变量我做了 this.myVariableName == data.isApproved 但这没有用我认为因为订阅需要一些时间这是错误的逻辑。
[解决方案]:下面的评论解决了它,我真傻我没有马上使用它(:解决方案>为什么简单的 if 语句不够?(数据)=> {if(!data.isApproved) this.electronicInvoiceService.getElectronicInvoice(this.invoiceId).subscribe();}
其他订阅中的订阅不被视为 RxJs 中的最佳实践。
我认为更惯用的解决方案可能如下
this.electronicInvoiceService.getElectronicInvoice(this.invoiceId)
// here you start a pipe, i.e. a series of operations that transform the source stream
.pipe(
// concatMap is the first operator which basically says: wait for upstream
// to notify and then take the data notified and return a new Observable
concatMap( data => {
// here you put your if logic
if (!data.isApproved) {
// return the other Observable
return this.electronicInvoiceStagingService.getElectronicInvoice(this.invoiceId)
}
// otherwise return EMPY, which is an Observable that does nothing and just
// completes immediately
return EMPTY
})
)
// eventually you just subscribe to the one and only one stream you have created
// transforming, with the pipe, the original source stream
.subscribe(dataFromStagingService => {
// do anything you need to do with the data returned by the staging service call
})