“run”在添加效果时出错。也尝试过手动返回字符串、数字等,但不起作用。
效果代码:
@Effect()
getRoles$: Observable<Roles[]> = this.dataPersistence.fetch(CreateActionTypes.GetRoles, {
run: (action: GetRoles) => {
return this.formService
.getRoles()
.pipe(map(roles => {
return new RolesLoaded(roles);
}));
},
onError: (action: GetRoles, error) => {
console.error(error);
return new RolesLoadError(error);
}
});
错误消息:
ERROR in apps/app/src/app/+state/create.effects.ts(32,5):
error TS2322:
Type '(action: GetRoles) => Observable' is not assignable to type '(a: GetRoles, state?: CreatePartialState) => void | Action | Observable'.
Type 'Observable' is not assignable to type 'void | Action | Observable'.
最佳答案
@Effect
应该返回一个Action
的可观察值,而不是Action[]
,而只是Action
。
就您而言,这似乎是多个概念之间的混合。
这是@Effect
的示例
createProject$ = createEffect(() => this.actions$.pipe(
ofType(CREATE_PROJECT),
switchMap(({ name, id }) => this.myService.createProject(name, id).pipe(
concatMap(project => [
CREATE_PROJECT_SUCCESS(project),
NAVIGATE({ path: ['foo', project.id] })
]),
catchHttpError()
)),
));
关于javascript - 类型 'Observable<void>' 不可分配给类型 'void | Action | Observable<Action>' 。添加效果时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58828260/