【问题标题】:The type 'void' can not be assigned to the type 'ObservableInput <Action>'“void”类型不能分配给“ObservableInput <Action>”类型
【发布时间】:2018-10-19 20:11:21
【问题描述】:

当我尝试在我的效果中分派可观察的操作时,我收到下一个类型错误,如下所示:

 @Effect()
    rideSummary$: Observable<Action> = this.actions$.pipe(
        ofType<GetRideSummary>(RideActionTypes.GetRideSummary),
        map(action => action.payload),
        switchMap(payload => {
            const { ride, passenger } = payload;
            const allPassengers = [...ride.passengers, passenger];
            this.store.dispatch(new SetLoading);
            return this.directionService.computeRideRouteSummary(ride, allPassengers)
            .then((summary: RideRouteSummary) => {
                const updatedRoute = Geometry.newFromData({
                    type: Geometry.LINE_STRING,
                    coordinates: summary.route
                });

                const totalTime = summary.totalTime;
                const arrivalTime = ride.type === Ride.TYPE_TOWORK ?
                    ride.hour :
                    ride.hour + summary.totalTime;
                const departureTime = ride.type === Ride.TYPE_TOWORK ?
                    ride.hour - summary.totalTime :
                    ride.hour;
               this.store.dispatch(new UnsetLoading);
               return this.store.dispatch(new GetRideSummarySuccess({
                    updatedRoute,
                    totalTime,
                    arrivalTime,
                    departureTime
                }));
                }
            ).catch(error => {
                this.store.dispatch(new UnsetLoading);
                catchError(() => of(new HandleError(error)));
            });
        })
    );

类型错误说:

Unable to assign an argument of type "(payload: {ride: Ride; passenger: Passenger;}) => void" to the type parameter "(value: {ride: Ride; passenger: Passenger;}, index: number) => ObservableInput <Action> ".
The type 'void' can not be assigned to the type 'ObservableInput <Action>'.

我不确定代码有什么问题。似乎与promise 有关。有什么帮助吗?

【问题讨论】:

  • 我不知道 RxJS,如果你标记了发生错误的行,它会有所帮助,但在这种情况下,我很好地猜测了这个问题:a missing return before this.directionService.computeRideRouteSummary(...).

标签: angular typescript observable ngrx ngrx-store


【解决方案1】:

效果定义为@Effect(),这意味着它需要Action。 快速但肮脏的解决方法是将您的效果定义为@Effect({dispatch: false})。 更正确的解决方法是删除效果中的所有商店调度并返回一个操作。

一个简单的例子是:

switchMap(payload => {
  this.store.dispatch(new FooBar());
}

收件人:

switchMap(payload => {
  return new FooBar();
}

【讨论】:

  • 在这种情况下,当我返回操作时,会返回操作不兼容。承诺要求返回void。我该如何处理?
猜你喜欢
  • 1970-01-01
  • 2019-05-06
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 2019-01-04
  • 1970-01-01
  • 2017-04-05
相关资源
最近更新 更多