【问题标题】:Handle Actions after NGRX Effect处理 NGRX 效果后的操作
【发布时间】:2017-09-26 22:50:28
【问题描述】:

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

以下是 store 发送一个 effect 来保存报表并将其注入到 store 中的示例。

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

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 });
}

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

【问题讨论】:

    标签: javascript ngrx ngrx-effects


    【解决方案1】:

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

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

    基本设置

    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))) 
        ); 
    

    【讨论】:

    猜你喜欢
    • 2019-10-16
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 2018-04-06
    • 2019-11-01
    • 2017-08-01
    • 1970-01-01
    • 2018-01-11
    相关资源
    最近更新 更多