redux 未调用NgRx效应

eimct9ow  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(210)

我最近在我的一个项目中配置了NgRx,并为一个名为OrderModel的特定模型实现了action、reducer和effects。
但是当我调度一个动作,我配置了一个效果,这个效果就不会运行了。
有人知道为什么吗?
下面是我的OrderModel

export interface OrderModel {
  orderNumber?: number;
  destination?: LatLngLiteral;
  source?: LatLngLiteral;
  accepted?: boolean;
  sourceAddressText?: string;
  destinationAddressText?: string;
}

以下是操作:

export const addRawOrder = createAction('[Order] Add new orders', props<{orders: OrderModel[]}>());
export const addOrder = createAction('[Order] Add new orders', props<{orders: OrderModel[]}>());
export const setSelectedOrder = createAction('[Order] Set selected order', props<{order: OrderModel}>());
export const setSelectedOrderAccepted = createAction('[Order] Set selected order accepted', props<{order: OrderModel}>());

下面是reducer代码:

import {addOrder, addRawOrder, setSelectedOrder, setSelectedOrderAccepted} from "./order-actions";

export interface OrderState {
  orders: OrderModel[];
  selectedOrder: OrderModel | null;
}

const initialState: OrderState = {orders: [], selectedOrder: null};

export const orderReducers = createReducer(initialState,
  on(addRawOrder, (state, {orders}) => ({...state})),
  on(addOrder, (state, {orders}) => ({...state, orders: [...state.orders, ...orders]})),
  on(setSelectedOrder, (state, {order}) => ({...state, selectedOrder: order})),
  on(setSelectedOrderAccepted, (state, {order}) => ({...state, selectedOrder: {...order, accepted: true}}))
);

下面是Effect代码:

@Injectable()
export class OrderEffects {

  constructor(private actions$: Actions, private neshanService: NeshanService) {
  }

  getPosts$ = createEffect(() =>
    this.actions$.pipe(
      ofType(addRawOrder),
      switchMap((action) => {
        return this.neshanService.reverseGeocodingSourceDestinationOder(action.orders);
      }),
      map(orders => addOrder({orders: orders}))
    )
  )
}

下面是app.module.ts的imports部分的配置:

StoreModule.forRoot({orders: orderReducers}),
EffectsModule.forRoot([OrderEffects]),
idfiyjo8

idfiyjo81#

动作addRawOrder在reducer和effect中都被处理。NGRX开始处理reducer的动作,然后在没有reducer对该动作感兴趣的情况下处理效果。解决方案是为效果创建一个不同的动作,然后addRawOrder

相关问题