我在使用ngrx时遇到了一些问题,我在尝试做一个简单的效果时遇到了错误。我得到的错误是类型为“MonoTypeOperatorFunction<LoadContainerAction>
”的参数不能分配给startWith行上类型为“OperatorFunction<Action, Action>
”的参数。
@Injectable()
export class Effects {
@Effect()
loadCollection$: Observable<Action> = this.actions$
.pipe(
ofType(ActionTypes.LOAD_CONTAINER),
startWith(new LoadContainerAction()),
switchMap(() =>
this.apiService.get<Container[]>("books")
.then(items => new LoadContainerSuccessAction(items))
.catch(error => of(new LoadContainerFailAction(error)))
)
);
constructor(private actions$: Actions, private apiService: ApiService) { }
}
这就是ActionTypes和LoadContainerAction的样子:
export enum ActionTypes {
LOAD_CONTAINER = "[container] load",
LOAD_CONTAINER_SUCCESS = "[container] load success",
LOAD_CONTAINER_FAILURE = "[container] load fail",
}
export class LoadContainerAction implements Action {
readonly type = ActionTypes.LOAD_CONTAINER;
}
这些是我的(相关)导入:
import { Injectable } from "@angular/core";
import { Action } from "@ngrx/store";
import { Effect, Actions, ofType } from "@ngrx/effects";
import { Observable, of } from "rxjs";
import { startWith, switchMap } from "rxjs/operators";
我使用的是rxjs:6.1.0和ngrx:6.0.0-beta.1
答案 0 :(得分:0)
ofType<LoadContainerAction>(ActionTypes.LOAD_CONTAINER),
似乎已经解决了问题,而不仅仅是ofType(...)