NgRx - 从后端获取错误验证并传递给组件
NgRx - Get error validation from backend and pass to component
我试图从 api 获取错误消息并显示在我的表单输入中,以便用户可以看到正在提交的数据有什么问题。
来自API的回复:
{
"message": "The given data was invalid.",
"errors": {
"name": [
"This name is already in use."
]
}
}
用户-form.component.ts
this.store.dispatch(new actions.CreateUser(user));
user.effect.ts
@Effect()
CreateUser$: Observable<Action> = this.actions$.pipe(
ofType(UserActions.CREATE_USER),
map((action: UserActions.CreateUser) => action.payload),
switchMap(payload => {
return this.userService.save(payload).pipe(
map((user: User) => {
return new UserActions.CreateUserSuccess(user);
}),
catchError(err => {
return of(new UserActions.CreateUserFail());
})
);
})
);
如何获取该错误并将其传回我的组件?
我是否应该喜欢效果内部并将其订阅到等待 CreateUserFail 错误的操作?我不确定这是否是一个好的做法,因为它会听取所有类型的 Actions。
按如下方式更改 catchError 应该就足够了:
catchError(err => {
return new UserActions.CreateUserFail(err);
})
也考虑使用隐式return:
map( (user: User) => new UserActions.CreateUserSuccess(user) ),
catchError( err => new UserActions.CreateUserFail(err) )
我们构建了一个选择器并订阅了该选择器。
效果
@Effect()
createProduct$: Observable<Action> = this.actions$.pipe(
ofType(productActions.ProductActionTypes.CreateProduct),
map((action: productActions.CreateProduct) => action.payload),
mergeMap((product: Product) =>
this.productService.createProduct(product).pipe(
map(newProduct => (new productActions.CreateProductSuccess(newProduct))),
catchError(err => of(new productActions.CreateProductFail(err)))
)
)
);
减速器
case ProductActionTypes.CreateProductFail:
return {
...state,
error: action.payload
};
选择器
export const getError = createSelector(
getProductFeatureState,
state => state.error
);
组件
// Watch for changes to the error message
this.errorMessage$ = this.store.pipe(select(fromProduct.getError));
模板
<div *ngIf="errorMessage$ | async as errorMessage" class="alert alert-danger">
Error: {{ errorMessage }}
</div>
您可以在此处找到完整的示例:https://github.com/DeborahK/Angular-NgRx-GettingStarted/tree/master/APM-Demo4
我试图从 api 获取错误消息并显示在我的表单输入中,以便用户可以看到正在提交的数据有什么问题。
来自API的回复:
{
"message": "The given data was invalid.",
"errors": {
"name": [
"This name is already in use."
]
}
}
用户-form.component.ts
this.store.dispatch(new actions.CreateUser(user));
user.effect.ts
@Effect()
CreateUser$: Observable<Action> = this.actions$.pipe(
ofType(UserActions.CREATE_USER),
map((action: UserActions.CreateUser) => action.payload),
switchMap(payload => {
return this.userService.save(payload).pipe(
map((user: User) => {
return new UserActions.CreateUserSuccess(user);
}),
catchError(err => {
return of(new UserActions.CreateUserFail());
})
);
})
);
如何获取该错误并将其传回我的组件?
我是否应该喜欢效果内部并将其订阅到等待 CreateUserFail 错误的操作?我不确定这是否是一个好的做法,因为它会听取所有类型的 Actions。
按如下方式更改 catchError 应该就足够了:
catchError(err => {
return new UserActions.CreateUserFail(err);
})
也考虑使用隐式return:
map( (user: User) => new UserActions.CreateUserSuccess(user) ),
catchError( err => new UserActions.CreateUserFail(err) )
我们构建了一个选择器并订阅了该选择器。
效果
@Effect()
createProduct$: Observable<Action> = this.actions$.pipe(
ofType(productActions.ProductActionTypes.CreateProduct),
map((action: productActions.CreateProduct) => action.payload),
mergeMap((product: Product) =>
this.productService.createProduct(product).pipe(
map(newProduct => (new productActions.CreateProductSuccess(newProduct))),
catchError(err => of(new productActions.CreateProductFail(err)))
)
)
);
减速器
case ProductActionTypes.CreateProductFail:
return {
...state,
error: action.payload
};
选择器
export const getError = createSelector(
getProductFeatureState,
state => state.error
);
组件
// Watch for changes to the error message
this.errorMessage$ = this.store.pipe(select(fromProduct.getError));
模板
<div *ngIf="errorMessage$ | async as errorMessage" class="alert alert-danger">
Error: {{ errorMessage }}
</div>
您可以在此处找到完整的示例:https://github.com/DeborahK/Angular-NgRx-GettingStarted/tree/master/APM-Demo4