我的组件正在调用动作,并使用@Effect打开对话框。对话框将数据发送回@Effect。我可以在@Effects中使用.afterClosed()来查看数据,但我不知道如何使用.afterClosed()将其传递到组件。
这是组件调用对话框的方式:
this.store.dispatch(new fromWorkspace.ShowDialog());
这里是效果中的对话框:
@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));
})
);
这里是对话框如何发送回数据:
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);
}
}
返回组件,这是我尝试使用.afterClose()的方式:
public dialogRef: MatDialogRef<PersonSelectorComponent>
//this does not work
this.assigneeDialogRef.afterClosed().subscribe(result => console.log(result));
通常,从效果中,您将调度一个动作,并生成结果数据,这些数据将通过化简器,然后最终存储在数据存储中。从那里,您的组件将被订阅到数据存储(通过选择器),并将以这种方式获取更新的数据。
如果您使用效果直接获取数据并将其返回到组件中而不将其放入存储中,那么我将完全不使用效果。我只需要直接调用该对话框即可得到结果并使用它来做我想做的事情。
因此,我采取了如下行动/减少方法:
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.`))
);
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);