ngrx效果使类型错误无法分配

时间:2019-02-26 06:56:09

标签: angular7 ngrx-effects

这是我的效果文件,

import { Injectable } from "@angular/core";
import { Action } from "@ngrx/store";
import { Actions, Effect, ofType } from "@ngrx/effects";
import { Observable, of } from "rxjs";
import { map, mergeMap, switchMap, catchError } from "rxjs/operators";
import { ModelCourse } from "./../models";
import { CourseService } from "./../services/course.service";
import { 
            courseActionTypes, 
            LoadCourse, 
            LoadCourseSuccess, 
            LoadCourseFail, 
            UpdateCourse,
            UpdateCourseSuccess,
            UpdateCourseFail } from "./course.actions";

@Injectable() 
export class CourseEffects {

    constructor(private courseService:CourseService, private actions:Actions){}

    @Effect()
        LoadCourse = this.actions.pipe( 
            ofType(LoadCourse.TYPE),
            mergeMap((action:LoadCourse) => this.courseService.getCourse().pipe(
                map((courses:ModelCourse[]) => {
                    return new LoadCourseSuccess(courses);
                }),
                catchError(err => of(new LoadCourseFail(err)))
            ))

        )

    @Effect()
        UpdateCourse = this.actions.pipe(
            ofType(UpdateCourse.TYPE),
            map((action:UpdateCourse) => action.payload),
            mergeMap((course:ModelCourse) => 
                this.courseService.updateCourse(course).pipe(
                    map((course) => (new UpdateCourseSuccess(course))),
                    catchError(err => (new UpdateCourseFail(err)))
                )
            )
        )

}
从上面的

,我收到以下错误。有人在这帮我吗?

ERROR in src/app/setup-config/state/course.effects.ts(41,24): error TS2322: Type 'UpdateCourseFail' is not assignable to type 'ObservableInput<{}>'.
  Property '[Symbol.iterator]' is missing in type 'UpdateCourseFail' but required in type 'Iterable<{}>'.

1 个答案:

答案 0 :(得分:0)

这是我的错字,我使用of运算符更新了catchError。现在工作正常。

这是更新的代码:

 catchError(err => of(new UpdateCourseFail(err)))

谢谢!