Using NGRX Effects is recommended . When you implement NGRX Effects with the Store, any HTTP side effects are handled by effects, which in turn will use the store action to update the data. The effect listens for the action and uses the Action payload to perform the side effect (HTTP). When an Effect ends, it invokes a new action (either an Action for success or an action for failure) with a new payload, thereby updating the data in the Warehouse.
In the example in the Effects Documents, it shows the Effect for Login
:
@Injectable()
export class AuthEffects {
constructor(
private http: Http,
private actions$: Actions
) { }
@Effect() login$ = this.actions$
.ofType('LOGIN')
.map(action => JSON.stringify(action.payload))
.switchMap(payload => this.http.post('/auth', payload)
.map(res => ({ type: 'LOGIN_SUCCESS', payload: res.json() }))
.catch(() => Observable.of({ type: 'LOGIN_FAILED' }))
);
}
Login
. Login
, HTTP POST. HTTP POST , LOGIN_SUCCESS
json , LOGIN_FAILED
.
, , HTTP. , Effect Store, , , .
, .