3

我是在我的角度应用程序中使用 NGRX/RXJS 的新手,我有一种情况,我必须从我的组件中调度一个操作,如果属性为空,它会从 API 获取数据并更新 Store_1 并设置一个属性,然后调度另一个操作,该操作使用来自 Store_1 的数据在 Store_2 中执行某些功能,并在操作 1 完成后更新状态并填写“pluralconfig”。这是我想出的代码,但我不认为/相信这是最有效的方法,如果我正确使用运算符。

  if(isEmpty(definition.pluralconfig == null))
     {
         this.store$.dispatch(new FormDBAction(definition.formId));

         let id = definition.id;
         this.formLoadSubscription = this.store$.select(getOrderById(id))
         .filter(v => v.pluralconfig != null).subscribe(() =>{

            this.store$.select<TosModel>(getOrderById(id)).subscribe(updatedDefinition => {
              this.store$.dispatch(new OrderDetailAction(updatedDefinition));
            });             
         });
     }
     else{
      this.store$.dispatch(new OrderDetailAction(definition));
     }
4

1 回答 1

3

所有这些逻辑都应该在效果上类似于

//Effects file
@Effect()
loadOrderDetail$: = this.actions$.pipe(
ofType(LOAD_ORDER_DETAIL),
flatMap(action => {
    return this.store$.select<TosModel>(getOrderById(action.id)).pipe(first());
})
map(definition => definition.pluralconfig ? new FormDBAction(definition.formId): new OrderDetailAction(definition)})
)

@Effect()
formDb$: = this.actions$.pipe(
ofType(FORM_DB_ACTION),
switchMap(action => {
    ///call api and fire  new FormDbActionSuccess(response.definition) which reducer will store
})

@Effect()
onSuccessOrderDetail$: = this.actions$.pipe(
ofType(FORM_DB_ACTION_SUCESS),
map(action => new OrderDetailAction(action.definition))

//Component
definition$ = this.store.select(getOrderById(id));
store.dispatch( new LoadOrderDetail(id));

看看https://github.com/ngrx/platform/tree/master/projects/example-app中的 ngrx 示例应用程序 那是我学到最多的

于 2018-06-05T22:06:25.013 回答