javascript - 处理 NGRX 效果后的操作

标签 javascript ngrx ngrx-effects

我有一个保存报告的 NGRX Effect,保存报告后我想重置表单并显示报告已保存的通知。

下面是商店调度效果以保存报告并将其注入(inject)商店的示例。

保存并插入后,我想重置表单并向用户显示通知。

onSubmit(): void {
  // Gather the fields from the form and
  // dispatch the new report event which will
  // save the report and insert it into the store
  const formModel = this.reportForm.value;
  this.store.dispatch(new AddReport(formModel));

  // After the report is saved, reset the form and
  // display a notification to the user it was saved
  this.reportForm.markAsPristine();
  this.snackbar.open('Report Saved!', null, { duration: 700 });
}

问题是我只想重置表单并在后端保存报告时显示通知。实现这一目标的最佳方法是什么。

最佳答案

效果应该返回一个新的操作。

您让effect进行API调用,然后如果成功,您将返回一个操作,该操作将命中reducer以重置表单,然后还调用一个effect来发送通知。

基本设置

reducers:
   successfullSubmit:
     const state = state.form = resetedform
     return state

effects
   formSubmit
     api.submitform
       on success return successfullSubmit
     catch
       on fail return submitFail
   successFullSubmit
     display notification

这就是为表单提交编写效果的方式

  @Effect()
  formSubmit$: Observable<Action> = this.actions$
    .ofType(actions.formSubmit)
    .map(toPayload)
    .switchMap(formStuffs =>
      this.api.submitForm(formStuffs)
        .map(() => new FormSubmitSuccess())
        .catch(error => of(new FormSubmitError(error))) 
    ); 

关于javascript - 处理 NGRX 效果后的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46436942/

相关文章:

javascript - 检查对象中逗号分隔的字符串

javascript - 如何读取xml中另一个标签下标签的属性

rxjs - 链接 RxJS map 运算符和 ngrx 效果问题

node.js - 如何等待发货完成后再从商店挑选。 Ngrx相关问题

RxJs:如何将两个 observable 组合成一个不同类型的新 observable

javascript - 有没有办法设置 slimbox 弹出窗口的最小宽度?

javascript - 验证后传递脚本

angular - 如何在map之前在rxjs中保存值

angular - reducer Action 中的 ngrx 有效负载未编译

angular - 如何在 Angular 2 的 ngrx 效果中访问参数?