从效果/缩小器/动作模式获取新数据

时间:2018-11-14 03:02:20

标签: angular typescript ngrx-store

好的,所以我似乎无法弄清楚如何更有效地做到这一点。坦率地说,我并不是很了解这种模式,但是我陷入了深渊。因此,我有一个解析器,可以为表单提供适当的计划。但是,由于要浏览的页面的缘故,我有效地完成了轻载的计划,该计划存储在状态中。因此,当发生负载而不是返回完全杂交的对象时,我得到的负载很轻。我唯一想出的解决方法是先将其从状态中删除。这似乎很愚蠢。如何配置效果以进行删除/加载,也许可以通过传递参数?

export class PlanResolver implements Resolve<Observable<Plan>> {

  public plan$: Observable<Plan>;

  constructor(public store: Store<fromRoot.State>) {
    this.plan$ = store.select<Plan>(fromRoot.getSelectedPlan);     
  }

  resolve(route: ActivatedRouteSnapshot) {
    let id = +route.paramMap.get('id');

    console.info(id);

    this.store.dispatch(new PlanActions.Remove(id));  //This seems silly
    this.store.dispatch(new PlanActions.Load(id));

    return of(this.plan$);
  }

减速器:

case PlanActionTypes.LoadSuccess: {
  return adapter.addOne(action.payload, {
    ...state,
    selectedPlanId: state.selectedPlanId,
  });
}

case PlanActionTypes.Load: {
  return {
    ...state,
    selectedPlanId: action.payload,
  };
}

效果:

@Effect()
  loadPlan$: Observable<Action> = this.actions$
    .ofType<Load>(PlanActionTypes.Load).pipe(
      switchMap(action => this.planService.getPlan(action.payload)),
      map((plan: Plan) => new LoadSuccess(plan)),
      catchError(err => {
        toastr.error(`Could not load plan.`); 
        console.error(err);
        return of(new LoadFail(err));
      })
    );

操作:

 export class Load implements Action {
  readonly type = PlanActionTypes.Load;

  constructor(public payload: number) {}
}

export class LoadSuccess implements Action {
  readonly type = PlanActionTypes.LoadSuccess;

  constructor(public payload: Plan) {}
}

export class LoadFail implements Action {
  readonly type = PlanActionTypes.LoadFail;

  constructor(public payload: any) {}
}

编辑:    所以我越来越 计划{    一个值    b:价值    c-z:空 }

我期待 计划{  一个值  ....  z:值 }

先移除即可得到预期的结果。

0 个答案:

没有答案