2017-10-11 94 views
-1

我不是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); 
    } 

回答

0

感谢的人谁看了一下.. 回答这个问题是如果没有任何东西需要保存,则返回空的可观察对象。

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

谢谢你们..快乐编码..