Pipe and Tap VS subscribe with ngxs

My answer is in two parts... What you asked, and what you need 😉. The values of an Observable only flow through the pipe operators when there is an active subscription. That is why you are seeing this behaviour. So you should do something like this:

this.store.select(state => state.auth.authUser).pipe(
  take(1),
  tap((data) => {
    console.log('[Tap] User Data', data)
  })
).subscribe();

But what it seems that you are looking for is a state snapshot. You can do this as follows:

let data = this.store.selectSnapshot(state => state.auth.authUser);
console.log('[selectSnapshot] User Data', data);

tap (or do in the old version of RxJS) -> Transparently perform actions or side-effects, such as logging (as definition already says).

But, in your case, you forgot to "subscribe()" to your Observable and that's why you don't see your "User Data" in the Console.

On the other hand, in the second case, you already subscribe to an Observable.


Got it! I have to append subscribe(). So it is:

this.store.select(state => state.auth.authUser).pipe(
  take(1),
  tap((data) => {
    console.log('[Tap] User Data', data)
  })
).subscribe();