angular - 具有非类操作的 Angular Nrwl Nx 数据持久性
问题描述
是否可以通过 Nx 数据持久性使用非基于类的操作?我在文档中找不到任何内容。这是我到目前为止所尝试的:
run: (action, state) => {
const booking: Booking = action.booking;
return this.httpClient.post<FirebasePostResponse>('https://foo/bar.json', booking).pipe(
map((res, err) => {
return bookingsAddOneSuccess({booking});
})
);
},
这给了我一个类型不匹配的错误。我想一种解决方法是自己使用@Effect({dispatch: false})
run 方法并从中分派而不返回任何内容。但也许有更好的方法,而不会滥用效果?
解决方案
NgRx 现在具有动作创建者:
export const addOne('[Bookings] Add One', props<{ booking: BookingsEntity }>());
export const addOneSuccess('[Bookings] Add One Success', props<{ booking: BookingsEntity }>());
与 Nx 的数据持久性功能配合良好:
addOne = createEffect(() =>
this.actions.pipe(
ofType(BookingsActions.addOne),
pessimisticUpdate({
// provides an action
run: ({ booking }) => {
// update the backend first, then dispatch an action
// that will update the client side
return this.httpClient.post<FirebasePostResponse>(
'https://foo/bar.json',
booking
).pipe(
map((res, err) => {
return BookingsActions.bookingsAddOneSuccess({ booking });
})
);
},
onError: ({ booking }, error) => {
// we don't need to undo the changes on the client side.
// we can dispatch an error, or simply log the error here and return `null`
return null;
},
})
)
);
推荐阅读
- elasticsearch - 通过 elasticsearch 搜索字符串关键字
- amazon-web-services - AWS 允许跨账户 EKS 集群从 ECR 拉取镜像
- c# - 如何从嵌套类通过依赖注入服务进行访问?
- java - 如何使用封装的数据在 Java 中创建 RSA 签名
- python - 如何在pySimpleGUI中添加可以切换的堆叠小部件
- c - 如何使用 libpng 库获取 16 位深度的灰度图像的像素值?
- java - JavaFX & Eclipse -> 舞台未打开
- php - 数据表与 jquery Laravel 8 冲突
- javascript - 如何在节点 16 中使用顶级等待?
- c++ - 如何将控制台输出对齐到小数点而不是左或右