Ich versuche, ngrx/Effekte zu verstehen. Ich habe eine einfache Funktion eingebaut, die bei jedem Klick um 1 erhöht. Aber es geht in eine Endlosschleife, wenn geklickt wird, nicht sicher, was los ist. Ich bin sicher, ich mache einen dummen Fehler.Unendliche Schleife mit ngrx/Effekte
monitor.effects.ts
@Injectable()
export class MonitorEffects {
@Effect()
compute$: Observable<Action> = this.actions$
.ofType(monitor.ActionTypes.INCREMENT)
.map((action: monitor.IncrementAction) => action.payload)
.switchMap(payload => {
return this.http.get('https://jsonplaceholder.typicode.com/users')
.map(data => new monitor.IncrementAction(payload+1))
.catch(err => of(new monitor.InitialAction(0)))
});
constructor(private actions$: Actions, private http:Http) {};
}
monitor.component.ts
ngOnInit() {
this.storeSubsciber = this
.store
.select('monitor')
.subscribe((data: IMonitor.State) => {
this.value = data.value;
this.errorMsg = data.error;
this.currentState = data.currentState;
});
}
increment(value: number) {
this.store.dispatch(new monitorActions.IncrementAction(value));
}
monitor.reducer.ts
export const monitorReducer: ActionReducer<IMonitor.State> = (state = initialState, action: Actions) => {
switch (action.type) {
case ActionTypes.INCREMENT:
return Object.assign({}, { value: action.payload, currentState: ActionTypes.INCREMENT, error: null });
...
...
...
default:
return Object.assign({}, { value: 0, currentState: ActionTypes.INITIAL, error: null });
}
}
Wenn Sie die Aktionen '.ofType (monitor.ActionTypes.INCREMENT) beobachten' und von Ihrem Effekt aus versenden Sie dann eine Aktion 'monitor.ActionTypes.INCREMENT' (von diesem Teil nehme ich an:' monitor.IncrementAction (payload + 1) '), dann wird der Effekt wieder ausgelöst, und wieder und wieder, ... – Maxime
Ja, das war das Problem. Vielen Dank! – Sudhakar
Ich habe eine richtige Antwort gegeben, falls jemand ein ähnliches Problem hat und von Ihrem Beispiel nicht versteht. Froh, dass es an deinem Projekt arbeitet! Bitte :) – Maxime