在我的项目保存在Firebase中时需要帮助来调用副作用操作。
vehicle.service.ts:
...
public addVehicle(vehicle: Vehicle, success, error) {
return this.vehicles$.push(vehicle).then(success).catch(error);
}
vehicle.effects.ts:
@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
))
答案 0 :(得分:1)
添加中间动作
@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操作。
在reducer里面我只有PERFORM_ACTION_DONE的处理程序。我有状态的特殊减速器,在那里我听PERFORM_ACTION_START和PERFORM_ACTION_DONE