【问题标题】:How to set real names for forkJoin responses?如何为 forkJoin 响应设置实名?
【发布时间】:2018-08-26 14:54:54
【问题描述】:

当我订阅 forkJoin 响应时,我会通过索引获得响应数组,具体取决于加入请求的数量:

forkResponse[0]
forkResponse[1]

this.loadComponentData().subscribe((forkResponse) => {
 console.log(forkResponse); 
});

是否可以设置实名而不是索引?我的意思是这样的:

forkResponse['numbers']
forkResponse['dogs']

如果一个请求落空,我会提供什么?这个forkResponse['dogs'] 会是空吗?

【问题讨论】:

    标签: angular rxjs angular2-observables


    【解决方案1】:

    我们可以使用自定义索引值进行分配,而不是将 observable 推送到数组中。

    服务中

    loadComponentData(){
       const observableArr = [];
       observableArr['numbers'] = this.getDataById(1);
       observableArr['dogs'] = this.getDataById(2);
      return Observable.forkJoin(observableArr);
    }
    
    getDataById() : Observable<any> {
       return of(true);
    }
    

    【讨论】:

    • 如果一个请求落空,我将给予什么?这个 forkResponse['dogs'] 会为空吗?
    • 谢谢,我接受你的回答,也请告诉我如何处理错误?
    • 所以我的意思是我不喜欢这种方式if (forkResponse['numbers'] !== null) {// do }
    • 如果你不喜欢个别的 Cond.你可以做 forEach 和基于键。您可以验证值是否为空。
    • 你能举例说明吗?
    猜你喜欢
    • 2012-04-16
    • 1970-01-01
    • 2021-04-24
    • 2015-08-23
    • 1970-01-01
    • 2014-05-04
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多