Angular Nrwl Nx 数据持久性与非 class 操作

Angular Nrwl Nx Data Persistence with non-class Actions

是否可以将非 class 操作与 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}) 并自己从 运行 方法发送而不返回任何内容。但也许有更好的方法,不会滥用 Effects?

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;
      },
    })
  )
);