I have a back-end service that returns user details that I need on multiple Angular components. My problem is that it's obviously been called multiple times. Here is my get request:
getUser(): Observable<User> {
if (this.user) {
return Observable.of(this.user);
} else {
return this.http
.get<User>(BACKENDPATH)
.map((user: User) => new User().deserialise(user))
.do((user) => {
this.user = user;
console.log(this.user.getUserName());
return this.user;
});
}
}
I have tried the following methods after the get part of the function but none have worked:
- .publishReplay(1).refCount()
- .shareReplay()
- .shareReplay(1)
These methods have worked for others online so I'm guessing that I am doing something wrong with the implementation of them but I can't figure out what.