从@ngrs/效果代码内部调用中间操作的正确方法是什么?



需要帮助来调用副作用操作,同时我的项目保存在Firebase中。

车辆服务:

...
public addVehicle(vehicle: Vehicle, success, error) {
  return this.vehicles$.push(vehicle).then(success).catch(error);
}

车辆效果:

    @Effect()
    addVehicle$ = this.actions$
    .ofType(VehicleActions.ADD_VEHICLE)
    // NEED TO CALL: this._statusActions.dataLoading('adding new vehicle...')) 
    .switchMap(action => 
        Observable.fromPromise(
            this._vehicleService.addVehicle(action.payload,
                suc => this._statusActions.dataAdded(action.payload.name),
                err => this._statusActions.dataNotSaved(action.payload.name)
            ) // I WANT TO CALL, NOT RETURN ONE OF THEM
    ))

添加中间操作

 @Effect()
    addVehicleStatusChange$ = this.actions$
    .ofType(VehicleActions.ADD_VEHICLE)
    .switchMap(this._statusActions.dataLoading('adding new vehicle...')) 
    // return action type ADD_VEHICLE_START

 @Effect()
    addVehicle$ = this.actions$
    .ofType(VehicleActions.ADD_VEHICLE_START)
    .switchMap(action => 
        Observable.fromPromise(
            this._vehicleService.addVehicle(action.payload,
                // Success return action ADD_VEHICLE_SUCCESS
                // Error return ADD_VEHICLE_ERROR
           ) 
    ))
 @Effect()
    addVehicleStatusChangeSuccess$ = this.actions$
    .ofType(VehicleActions.ADD_VEHICLE_SUCCESS)
     .switchMap(suc => this._statusActions.dataAdded(action.payload.name)),
    // return action type some random action you don't care 
 @Effect()
    addVehicleStatusChangeError$ = this.actions$
    .ofType(VehicleActions.ADD_VEHICLE_ERROR)
     .switchMap( =err => this._statusActions.dataNotSaved(action.payload.name))
    // Return some random action. 

我通常做的是从组件中分派PERFORM_ACTION。在效果中侦听该操作并返回新的操作PERFORM_ACTION_START。

一旦操作完成,返回操作PERFORM_ACTION_DONE。

在减速器内部,我只有PERFORM_ACTION_DONE处理程序.我有特殊的状态减速器,在那里我听PERFORM_ACTION_START和PERFORM_ACTION_DONE

最新更新