0

我正在尝试通过 NgRx 效果检索本地存储数据。PATIENT_FETCH调度动作以执行效果。但是在调用方法中的调度方法时会无限调用ngOnInit

这是stackblitz演示。如果您取消注释patient.component.ts给定链接的第 24 行,那么会无限次调用 reducer。我可能在 NgRx 实现中遗漏了一些东西。

4

1 回答 1

0

问题是效果。

 @Effect()
    patient: Observable<any> = this.actions.pipe(
        ofType(PatientActionTypes.PATIENT_FETCH),
        tap((action: ActionTemplate<PatientFetch>) => {
            const patient: any = this.storageService.get(STORAGE_KEY, {});
            return patientActionFactory.create<PatientFetchSuccess>(PatientActionTypes.PATIENT_FETCH_SUCCESS, patient);
        })
    );

上面不断地调度PATIENT_FETCH动作,并再次拿起它 - 导致无限循环。通过查看您可能想要返回PATIENT_FETCH_SUCCESS操作的代码。因此,您必须将运算符替换为tap运算map符。

于 2021-01-25T20:00:07.193 回答