如何将params(action.payload)传递给ngrx/effects中的服务?

Raj*_*Raj 2 ngrx ngrx-effects angular6

我是ngrx-6的新手.效果将侦听操作"LOAD_COURSE_DETAILS",并应使用course_id(action.payload)调用服务.但我收到一个错误

"行动"类型中缺少"toFixed"属性.

但是,如果我执行console.log,我可以看到数据从组件传递到效果.

将params添加到效果中

如何将参数传递给服务?

提前致谢.


文件:效果

@Effect()
loadCourseDetails$ = this.actions$.pipe(
  ofType(CourseActionTypes.LOAD_COURSE_DETAILS),
  switchMap((action) => {
    console.log('in course effects', action);
    return this.courseService.getCourseDetails(action).pipe(
      map((data) => new fromCourse.LoadCourseDetailsSuccess(data))
    );
  })
);
Run Code Online (Sandbox Code Playgroud)

file:actions.ts(我的操作定义了有效负载)

export class LoadCourseDetails implements Action {
  readonly type = CourseActionTypes.LOAD_COURSE_DETAILS;
  constructor(public payload: Number) {}
}
export class LoadCourseDetailsSuccess implements Action {
  readonly type = CourseActionTypes.LOAD_COURSE_DETAILS_SUCCESS;
  constructor(public payload: ICourse) {}
}
Run Code Online (Sandbox Code Playgroud)

file:component.ts(dispatch action)

loadCourseDetails(id: Number) {
  console.log('dispatch course id', id);
  this.store.dispatch(new fromCourse.LoadCourseDetails(id));
}
Run Code Online (Sandbox Code Playgroud)

file:service.ts(由效果调用)

getCourseDetails(courseId: Number) {
    return this.http.get(`url/${courseId}.json`);
}
Run Code Online (Sandbox Code Playgroud)

tim*_*ver 7

你必须使用payload来自action.为了做到这一点,你必须填写ofType函数的泛型.

现在里面的动作switchMap足够聪明,知道它是一个LoadCourseDetails动作,因此也将知道有效载荷类型.

@Effect()
loadCourseDetails$ = this.actions$.pipe(
  ofType<LoadCourseDetails>(CourseActionTypes.LOAD_COURSE_DETAILS),
  switchMap((action) => {
    console.log('in course effects', action);
    return this.courseService.getCourseDetails(action.payload).pipe(
      map((data) => new fromCourse.LoadCourseDetailsSuccess(data))
    );
  })
);
Run Code Online (Sandbox Code Playgroud)

要获得更多效果,请查看开始使用ngrx/effects