带有非类操作的Angular Nrwl Nx数据持久性

时间:2019-09-24 18:54:49

标签: angular nrwl nrwl-nx

是否可以在Nx Data Persistence中使用基于非类的操作?我在文档中找不到任何内容。这是我到目前为止尝试过的:

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

1 个答案:

答案 0 :(得分: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;
      },
    })
  )
);