如何将参数 (action.payload) 传递给 ngrx/effects 中的服务?
How to pass params (action.payload) to service in ngrx/effects ?
我是 ngrx-6 的新手。
效果将监听动作 "LOAD_COURSE_DETAILS" 并且应该使用 course_id (action.payload) 调用服务。但是我收到一个错误
Property 'toFixed' is missing in type 'Action'.
但是,如果我这样做 console.log,我可以看到数据从组件传递到效果。
如何给服务传递参数?
提前致谢。
文件:效果
@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))
);
})
);
文件: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) {}
}
文件:component.ts(调度操作)
loadCourseDetails(id: Number) {
console.log('dispatch course id', id);
this.store.dispatch(new fromCourse.LoadCourseDetails(id));
}
文件:service.ts(被效果调用)
getCourseDetails(courseId: Number) {
return this.http.get(`url/${courseId}.json`);
}
您必须使用 action
中的 payload
。
为此,您必须填写 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))
);
})
);
更多效果用法,查看Start using ngrx/effects for this
我是 ngrx-6 的新手。 效果将监听动作 "LOAD_COURSE_DETAILS" 并且应该使用 course_id (action.payload) 调用服务。但是我收到一个错误
Property 'toFixed' is missing in type 'Action'.
但是,如果我这样做 console.log,我可以看到数据从组件传递到效果。
如何给服务传递参数?
提前致谢。
文件:效果
@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))
);
})
);
文件: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) {}
}
文件:component.ts(调度操作)
loadCourseDetails(id: Number) {
console.log('dispatch course id', id);
this.store.dispatch(new fromCourse.LoadCourseDetails(id));
}
文件:service.ts(被效果调用)
getCourseDetails(courseId: Number) {
return this.http.get(`url/${courseId}.json`);
}
您必须使用 action
中的 payload
。
为此,您必须填写 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))
);
})
);
更多效果用法,查看Start using ngrx/effects for this