当我尝试编辑我的用户时遇到问题。我决定在成功更新后再次获取所有用户。但是,如果出现错误,我不希望表单重置并且模式消失。我的问题是当我尝试更新用户并返回错误时。即使出现错误,ofActionSuccessful 仍然会触发。请在下面查看我的代码。
组件.ts
onEditUser(form: FormGroup) {
const formData = {
id: form.value.id,
name: form.value.name,
email: form.value.email
};
console.log(formData);
if (form.valid) {
swal({
title: 'Update',
text: 'Are you sure you want to update changes?',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#0CC27E',
cancelButtonColor: '#FF586B',
confirmButtonText: 'Confirm',
cancelButtonText: 'Cancel',
confirmButtonClass: 'btn btn-success btn-raised mr-5',
cancelButtonClass: 'btn btn-danger btn-raised',
buttonsStyling: false
}).then(result => {
console.log(result);
if (result.value) {
this.store.dispatch(new UpdateUser(formData));
this.actions$.pipe(ofActionSuccessful(UpdateUser)).subscribe(() =>
form.reset(),
this.modalReference.close()
);
}
});
}
}
状态.ts
@Action(UpdateUser)
updateTodo({ getState }: StateContext<UserStateModel>, { payload }: UpdateUser) {
return this.usersService.updateUser(payload).pipe(
tap((result) => {
console.log(result);
const state = getState();
this.store.dispatch(new GetUsers);
}),
catchError(err => {
console.log(err);
return throwError(err);
})
);
}