Home > Blockchain >  Angular rxjs Subject : subscribing to a subject after emitting values
Angular rxjs Subject : subscribing to a subject after emitting values

Time:10-21

I read an output

CodePudding user response:

The only reason your example subscribe is running is because you load the Header component before you start the login process/method. ngOnInit of component is called before login method in service.

Your code run like this not the way you have described in the quesiton:

user = new  Subject<User>();
subject.next('test');

// then this runs in component ngOnInit 
this.authService.user.subscribe((event) => {
  console.log(event);
});

// when you try to login from inside the component then it runs the login method with next.
login(email:string,password:string){.....
this.user.next(user);  // now this is triggered hence you don't loose the user response.
  • Related