0

是否可以通过 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 方法并从中分派而不返回任何内容。但也许有更好的方法,而不会滥用效果?

4

1 回答 1

1

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;
      },
    })
  )
);
于 2020-07-17T16:50:18.263 回答