使用新的 Ngrx 8 createEffect 模式时多次触发服务方法
Service methods firing multiple times when using new Ngrx 8 createEffect pattern
将一些功能效果切换到新的 createEffct()
模式后,他们调用的所有服务请求现在在我的控制台输出中触发两次。
操作如下:
export const getSetting = createAction(
'[Practitioner Settings] Get Setting',
props<{ name: string }>(),
);
下面是一个使用旧语法的 Effect 示例:
@Effect()
getSetting$: Observable<Action> = this.actions.pipe(
ofType(PractitionerSettingsActionTypes.GetSetting),
map((action: PractitionerSettingsActions.GetSetting) => action.name),
switchMap((name: string) => {
return this.practitionerSetingsService
.getSetting(name)
.pipe(
map(response => new PractitionerSettingsActions.GetSettingSuccess(response)),
)
}),
);
和新语法,基于以下示例:https://github.com/ngrx/platform/blob/master/projects/example-app/src/app/books/effects/collection.effects.ts:
@Effect()
getSetting = createEffect(() =>
this.actions.pipe(
ofType(PractitionerSettingsActions.getSetting),
mergeMap(({ name }) => {
return this.practitionerSetingsService
.getSetting(name)
.pipe(
map(response => PractitionerSettingsActions.getSettingSuccess({ setting: response })),
)
}),
)
);
我怀疑是 mergeMap
运算符导致了这种行为,因为这是两者之间的唯一区别,但我不确定为什么会导致这种情况,或者如何以另一种方式访问操作的有效负载.
除了将 Ngrx Actions、Reducers 和 Effects 更新为 v8 模式之外,我没有对我的应用程序进行任何其他更改,这会导致服务调用被执行两次。
如果不是 mergeMap,我应该使用什么运算符来访问操作负载?如果 mergeMap 是正确的,为什么我的效果 运行 不止一次?
如果您检查链接的示例,@Effect()
注释不存在,它存在于问题代码中。我认为这就是它以这种方式为您工作的原因
将一些功能效果切换到新的 createEffct()
模式后,他们调用的所有服务请求现在在我的控制台输出中触发两次。
操作如下:
export const getSetting = createAction(
'[Practitioner Settings] Get Setting',
props<{ name: string }>(),
);
下面是一个使用旧语法的 Effect 示例:
@Effect()
getSetting$: Observable<Action> = this.actions.pipe(
ofType(PractitionerSettingsActionTypes.GetSetting),
map((action: PractitionerSettingsActions.GetSetting) => action.name),
switchMap((name: string) => {
return this.practitionerSetingsService
.getSetting(name)
.pipe(
map(response => new PractitionerSettingsActions.GetSettingSuccess(response)),
)
}),
);
和新语法,基于以下示例:https://github.com/ngrx/platform/blob/master/projects/example-app/src/app/books/effects/collection.effects.ts:
@Effect()
getSetting = createEffect(() =>
this.actions.pipe(
ofType(PractitionerSettingsActions.getSetting),
mergeMap(({ name }) => {
return this.practitionerSetingsService
.getSetting(name)
.pipe(
map(response => PractitionerSettingsActions.getSettingSuccess({ setting: response })),
)
}),
)
);
我怀疑是 mergeMap
运算符导致了这种行为,因为这是两者之间的唯一区别,但我不确定为什么会导致这种情况,或者如何以另一种方式访问操作的有效负载.
除了将 Ngrx Actions、Reducers 和 Effects 更新为 v8 模式之外,我没有对我的应用程序进行任何其他更改,这会导致服务调用被执行两次。
如果不是 mergeMap,我应该使用什么运算符来访问操作负载?如果 mergeMap 是正确的,为什么我的效果 运行 不止一次?
如果您检查链接的示例,@Effect()
注释不存在,它存在于问题代码中。我认为这就是它以这种方式为您工作的原因