我已经在Angular 6 + Ngrx应用程序中编写了一个效果,但是会引发以下错误。
类型Observable<RouteSuccessAction>
的参数不能分配给类型<value: never, index: number> => ObservableInput<Action>
的参数
下面是代码段。
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
}
]
答案 0 :(得分:4)
您的switchmap不是应有的功能
switchMap(() => ...)