2017-11-11 72 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())