Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 组合测试,但仅针对一个发射_Angular_Ngrx_Ngrx Effects - Fatal编程技术网

Angular 组合测试,但仅针对一个发射

Angular 组合测试,但仅针对一个发射,angular,ngrx,ngrx-effects,Angular,Ngrx,Ngrx Effects,我正在做一个angular项目,使用最新版本的ngrx。我有一个效果,我想结合几个观察值,如下所示: loadSavedReplies=createEffect=>this.actions$.pipe 类型savedRepliesActions.SavedRepliesRequested, mergeMapaction=>CombineTest of action, this.store.pipeselectcurrentUser,filteru=>u!=无效的 , maps=>SavedRep

我正在做一个angular项目,使用最新版本的ngrx。我有一个效果,我想结合几个观察值,如下所示:

loadSavedReplies=createEffect=>this.actions$.pipe 类型savedRepliesActions.SavedRepliesRequested, mergeMapaction=>CombineTest of action, this.store.pipeselectcurrentUser,filteru=>u!=无效的 , maps=>SavedRepliesActions.SavedRepliesLoaded{ 答复:s.model, a:action.id } ; 我的问题是当当前用户发生更改时,CombineTest会不断发出信号。我想要的只是从CombineTest中得到一个发射。我只能简单地在store.pipe中添加take1,这样selectcurrentUser就不会再次触发CombineTest,但这是正确的方法吗。当我在特效中使用CombineTest时,我是否需要添加take1,而我只想获得一个发射。

是的,你可以使用CombineTest,但我认为操作符是你的最佳选择。无论如何,你需要额外的take操作符


zip将在两次可观察到的发射和完整的使用take运算符后发射

我看不出使用take运算符有什么问题。如果您只需要一个值,请使用它。如果你想在那里做额外的工作,也可以先使用。是的,我考虑过了,问题是LatestFrom不等待可观察对象发出一个值。例如,在我的例子中,selectCurrentUser不会立即发出值,而是在返回null以外的值后发出,然后源可观察操作不会发出任何东西,因此我最终没有得到任何发射。
loadSavedReplies = createEffect(() => this.actions$.pipe(
  ofType(SavedRepliesActions.SavedRepliesRequested),
  mergeMap((action)=>zip(of(action), this.store.pipe(select(currentUser),filter(...))).pipe(take(1)),
  map(([action,model]) => SavedRepliesActions.SavedRepliesLoaded({
    SaveReplies: s.model,
    a: action.id
  }))
));