0

I need to create a SessionService which will contain information about user and should act as a singleton across all components.

I would use it like:

this.sessionService.user$.subscribe(_ => {
 this.greetingsName = _.firstName;
});

It should be lazy loading: first call should get data over http and store it somewhere, any other subscription should replay this stored data.

Can you advice any technique / pattern / best practice for this in Angular / RxJS ?

Thanks!

Luke1988
  • 1,850
  • 2
  • 24
  • 42

1 Answers1

1

First of all, if you want to have a service that is a singleton you have two options:

  • set the providedIn property of the @Injectable() to "root"
  • include the service in the AppModule or in a module that is only imported by the AppModule

As for the implementation of the service, this can do the http request you need and save the data in a Subject. I would suggest you use a BehaviorSubject to store the data received from the http request.

Elidor00
  • 1,271
  • 13
  • 27