【问题标题】:switchMap in ngrx effects with Angular 6+ throws type errorAngular 6+的ngrx效果中的switchMap引发类型错误
【发布时间】:2019-06-27 09:52:37
【问题描述】:

我在我的 Angular 6 + Ngrx 应用程序中编写了一个效果,但是它引发了以下错误。

Observable<RouteSuccessAction> 类型的参数不能分配给<value: never, index: number> => ObservableInput<Action> 类型的参数

下面是代码sn-ps。

route.effects.ts

@Injectable()
export class RouteEffect {
    constructor(private actions: Actions, private routeService: RouteService) {
    }

    @Effect()
    getRoutes$: Observable<Action> = this.actions.pipe(
        ofType(RouteActions.RouteActionTypes.ROUTE_LOAD), switchMap(
            this.routeService.getRoutes().pipe(
                map(routes => new RouteActions.RouteSuccessAction(routes.route))
            )
        )
    );
}

route.actions.ts

import { Action } from '@ngrx/store';
import { Route } from '../model';
export enum RouteActionTypes {
    GET_ROUTE = '[GET ROUTE] GET',
    ROUTE_FAIL = '[ROUTE_FAIL] FAIL',
    ROUTE_SUCCESS = '[ROUTE_SUCCESS] SUCCESS',
    ROUTE_LOAD = '[ROUTE_LOAD] LOAD',

}

export class GetRouteAction implements Action {
    readonly type = RouteActionTypes.GET_ROUTE;

}
export class RouteSuccessAction implements Action {
    readonly type = RouteActionTypes.ROUTE_SUCCESS;
    constructor(public payload: Route[]) {
    }

}
export class RouteFailureAction implements Action {
    readonly type = RouteActionTypes.ROUTE_FAIL;
    constructor(public payload: any) {
    }

}
export type RouteActions = GetRouteAction | RouteSuccessAction | RouteFailureAction;

route.model.ts

export interface Route {
  'referringURL': string;
  'routeName': string;
  'visible': boolean;
}

服务返回以下响应

    [
        {
            "referringURL": "",
            "routeName": "route1",
            "visible": true
        },
        {
            "referringURL": "",
            "routeName": "route2",
            "visible": true
        },
        {
            "referringURL": "",
            "routeName": "route3",
            "visible": true
        }
]

【问题讨论】:

  • 请贴出与此错误相关的代码。
  • 丹尼尔,我已经发布了。

标签: angular6 ngrx


【解决方案1】:

你的 switchmap 不应该是一个函数

switchMap(() => ...)

【讨论】:

  • 哦!我的错。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 2021-05-19
  • 2019-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多