简体   繁体   English

如何从 MatDialog => Rxjs Effect => Component 传递数据?

[英]How do I pass data from MatDialog => Rxjs Effect => Component?

My component is calling an Action and using @Effect to open the Dialog.我的组件正在调用一个动作并使用@Effect 打开对话框。 The Dialog send the data back to the @Effect.对话框将数据发送回@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().我可以在@Effects 中使用.afterClosed() 查看数据,但我不知道如何使用.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:以下是 Dialog 如何发回数据:

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():回到组件这里是我尝试使用的方式。afterClose():

public dialogRef: MatDialogRef<PersonSelectorComponent>


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

Generally from an effect you would dispatch an action with the resulting data which would go through your reducer and then end up in your data store.通常,从效果出发,您将使用结果数据发送一个操作,该操作将通过您的减速器 go ,然后最终进入您的数据存储。 From there your component would be subscribed to the data store (via a selector) and would get the updated data that way.从那里您的组件将被订阅到数据存储(通过选择器)并以这种方式获取更新的数据。

If you're using an effect to directly get the data and return it to your component without putting it in the store then I wouldn't use an effect at all.如果您使用效果直接获取数据并将其返回到您的组件而不将其放入存储中,那么我根本不会使用效果。 I'd just call the dialog directly and get the result and do what I want with it.我只是直接调用对话框并获得结果并用它做我想做的事。

So, going forward with action/reducer approach I did as follow:因此,继续使用 action/reducer 方法,我执行以下操作:

  • created a new action "addPerson/addPersonSuccess" (to avoid subscribing to the data returned from the Dialog.创建了一个新的操作“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.`))
  );
  • then dealt with it in the reducer:然后在reducer中处理它:
 case WorkspaceActionTypes.AddPersonSuccess:

      return update(state, {
        person: {
          data: { $set: action.payload }
        }
      });
  • and included a Selector in the reducer as well:并在减速器中包含了一个选择器:
export const addPerson = createSelector(getWorkspaceState, (state: WorkspaceState) => state.person);
  • then back in the component call it in the constructor:然后回到组件中,在构造函数中调用它:
 this.person$ = this.store.select(fromWorkspace.addPerson);
  • now I can get the data by subscribing to the 'this.person$' observable.现在我可以通过订阅 'this.person$' observable 来获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM