2017-11-11 74 views
2

這實際上是自我記錄,但它可能對其他人有用。RxJS管道如何工作(可出租操作員)

所以,這裏的2個代碼和我的小膠質問題:

什麼是這兩個?:

@Effect() 
    loadRegistrationsFailed$: Observable<Action> = this.actions$ 
     .ofType(registrations.LOAD_FAIL) 
     .pipe(
      map(
       action => 
        new ShowErrorDialogAction({ 
         correlationId: new Guid(), 
         title: "Server is unreachable", 
         message: 
          "Can't load user registrations. Can't connect to the server" 
        }) 
      ) 
     ); 
``` 
and 
``` 
    @Effect() 
    loadRegistrationsFailed$: Observable<Action> = this.actions$ 
     .ofType(registrations.LOAD_FAIL) 
     .pipe(action => 
      of(
       new ShowErrorDialogAction({ 
        correlationId: new Guid(), 
        title: "Server is unreachable", 
        message: 
         "Can't load user registrations. Can't connect to the server" 
       }) 
      ) 
     ); 

回答

1

感謝勃蘭特B之間的差異,這裏的答案:

它與管道功能的工作方式有關。管道減少了傳遞給它的函數的數組。在之前的值中,它執行映射函數,該函數存儲您在內部傳遞給映射的函數。在第二個示例中,它執行immediatly的action =>並將其作爲管道的結果返回。因此,整個可觀察到的結果是其中得到由它產生的值的效果庫訂閱(動作)立即


從多魯什上同樣的問題的答案:

之間的差這兩個樣本是第一個將映射這些值的地方,第二個樣本將會用整數替換整個值,並忽略來源發出的任何東西。

寫第二個是

@Effect() 
loadRegistrationsFailed$: Observable<Action> = this.actions$ 
    .ofType(registrations.LOAD_FAIL) 
    .pipe(ob => ob.mergeMap(action => 
     of(
      new ShowErrorDialogAction({ 
       correlationId: new Guid(), 
       title: "Server is unreachable", 
       message: 
        "Can't load user registrations. Can't connect to the server" 
      }) 
     )) 
    ); 

既然你不使用的動作,你也可以使用mergeMapTo或mepTo正確的方法:

@Effect() 
loadRegistrationsFailed$: Observable<Action> = this.actions$ 
    .ofType(registrations.LOAD_FAIL) 
    .pipe(ob => ob.mergeMapTo(of(
      new ShowErrorDialogAction({ 
       correlationId: new Guid(), 
       title: "Server is unreachable", 
       message: 
        "Can't load user registrations. Can't connect to the server" 
      }) 
     )) 
    ); 

唯一可出租經營者補充的是,你可以寫.pipe(map())而不是.pipe(ob => ob.map())