1

My component is calling an Action and using @Effect to open the Dialog. The Dialog send the data back to the @Effect. I'm able to see the data using .afterClosed() in the @Effects, but I don't know how to get it to the component using .afterClosed().

Here is how the component is calling the dialog:

this.store.dispatch(new fromWorkspace.ShowDialog());

Here is the Dialog in the Effects:

  @Effect({ dispatch: false })
   showDialog$ = this.actions$.pipe(
    ofType(WorkspaceActionTypes.ShowDialog),
    map(action => {
      this.dialogRef = this.addPersonDialog.open(PersonSelectorComponent, {
        disableClose: false,
        hasBackdrop: true,
        restoreFocus: true,
        data: { }
      });
      // I can see the data here;
      this.dialogRef.afterClosed().subscribe(result => console.log(result));
    })
  );

Here is how the Dialog is sending data back:

constructor(@Inject(MAT_DIALOG_DATA) public data: any,
              public dialogRef: MatDialogRef<PersonSelectorComponent>) { }


 addPerson(event: MatAutocompleteSelectedEvent) {
    if (event.option.selected) {
      const person = event.option.value;
      if (person) {
      this.dialogRef.close(person);
      // data is correct here;
      console.log(person);
      }
    }

Back to the component here is how I'm trying to use .afterClose():

public dialogRef: MatDialogRef<PersonSelectorComponent>


//this does not work
this.assigneeDialogRef.afterClosed().subscribe(result => console.log(result));

4

2 回答 2

0

通常,从效果中,您将使用结果数据分派一个操作,该操作将通过您的减速器,然后最终进入您的数据存储。从那里您的组件将被订阅到数据存储(通过选择器)并以这种方式获取更新的数据。

如果您使用效果直接获取数据并将其返回到您的组件而不将其放入存储中,那么我根本不会使用效果。我只是直接调用对话框并获得结果并用它做我想做的事。

于 2019-11-19T17:14:02.770 回答
0

因此,继续使用 action/reducer 方法,我执行以下操作:

  • 创建了一个新的操作“addPerson/addPersonSuccess”(以避免订阅从 Dialog 返回的数据。
  addPerson$ = this.actions$.pipe(
    ofType(WorkspaceActionTypes.AddPerson),
    map(action => {
      return new AddPersonSuccess(action.payload);
    }),
    catchError(error => this.dispatchErrorHandleActions(new addPersonFailure(error),
            `Couldn't add person. Please try again later.`))
  );
  • 然后在reducer中处理它:
 case WorkspaceActionTypes.AddPersonSuccess:

      return update(state, {
        person: {
          data: { $set: action.payload }
        }
      });
  • 并在减速器中包含了一个选择器:
export const addPerson = createSelector(getWorkspaceState, (state: WorkspaceState) => state.person);
  • 然后回到组件中,在构造函数中调用它:
 this.person$ = this.store.select(fromWorkspace.addPerson);
  • 现在我可以通过订阅 'this.person$' observable 来获取数据。
于 2019-11-20T19:15:34.407 回答