【问题标题】:Switchmap on same function相同功能的开关图
【发布时间】:2017-10-11 19:33:11
【问题描述】:

我不是 rxjs 专家。

我有一个方法可以实际保存。

 this.saveProducts(this.boxes, 'BOXES')

但是还有另外两种不同类型的物品需要使用相同的方法进行保存,只是参数不同而已。

 this.saveProducts(this.containers, 'CONTAINER')

在我的组件中,我几乎没有其他独立的保存发生,所有这些都应该一一发生。

所以我的方法看起来像这样。

 return this.service.edit(materials)
        .do((data) => {
            this.materials= data;
        })
        .switchMap(() => this.customerService.addCustomer(this.id, this.customers))
        .switchMap(() => this.saveProducts(this.boxes, 'BOXES'))
        .switchMap(() => this.saveProducts(this.containers, 'CONTAINER'))
        .switchMap(() => this.saveProducts(this.books, 'BOOKS'))
        .do(() => {
         .......
            }

但是发生了什么是它永远不会调用第二个 saveProducts 方法,除非我从第一个返回

    private saveProducts(products: IProduct[], type: 
Type): Observable<any> {
        console.log(type);
    }

【问题讨论】:

    标签: rxjs angular2-services angular2-observables


    【解决方案1】:

    感谢任何看过它的人.. 这个问题的答案是如果没有可保存的,则返回一个空的 observable。

      if (products$.length === 0) {
            return Observable.of([]);
        }
    

    谢谢大家..快乐编码..

    【讨论】:

      猜你喜欢
      • 2012-09-08
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多