I'm having problems subscribing to an observer that's called in a different component.
My Angular 4 app has a navigation bar containing some of the user information. The user information is obtained from a http request to a server on login. This is managed with the LoginService. At logout we set the user to null.
logIn() {
return this.http
.get(this.apiUrl)
.map(response => response.json())
}
logOut() {
return Observable.of(null)
}
From the login component it's pretty easy to get the user details using the service.
logIn() {
this.loginService.logIn().subscribe((user) => {
console.log('Logged in')
this.user = user
console.log(this.user)
)
}
But I also need to get them from another component to set the values in the navbar. I think I need to use the navbar component to subscribe to the LoginService logIn() and logOut() functions, merge these observables, and if either of them trigger an event, update the user. But I can't subscribe to them and 2 days later I'm still stuck on this.
I've made a plunk to show the problem. When the login button is clicked, the user in the app.component should be set, and should be unset when the logout button is clicked.