NGRX 4 effects with angular 4.3 httpClient

1k views Asked by At

Hi Stackoverflow team!

I have this effect, and i'm using ngrx 4 with angular 4.3 httpClient on the service. The problem is that I could not see any examples on how to used them together. I saw that now, with httpclient we need to use the subscribe() method, so it is not clear to be, what to return, for example on the following code :

@Effect()
login$ = this.actions$
.ofType(Auth.LOGIN)
.map((action: Auth.Login) => action.payload)
.exhaustMap(auth =>
  this.authService
    .login(auth)
    .map(user => new Auth.LoginSuccess({ user }))
    .catch(error => of(new Auth.LoginFailure(error)))
);

So, i need to understand the logic on this line: .login(auth) Thanks so much

1

There are 1 answers

0
Reactgular On

The .exhaustMap is an operator that will subscribe to any observables that are returned by the callback code. It's going to keep listening until the observable is complete. You might want to add a .login(auth).take(1) if you know there will always be just one response.

@Effect declares a property variable as an Observable<action>. During bootstrapping the ngrx library will be the one that calls subscribe on all effects registered in your application.

That's why you don't see a subscribe call in any of the documentation. This is done for you by the library.