0

I have a service that I wanted to be singleton. According to the docs, I need to the providedIn as root, which I have done as shown below.

@Injectable({ providedIn: "root" })
export class SecurityService {
  constructor(private http: HttpClient) {
    console.log("SecServ created.");
  }
}

However, every time I navigate to a component that has the service injected, I can see the log in the console telling me that a new instance is created. I tried removing/adding the service in providers section of the module. It was added from start (by the CLI tools) despite it being obsolete approach (according to the docs).

How can I make the service behave as a singleton?

The only thing I can think of at the moment is to keep a provate reference to the service in the service and make it static and assignable to each new attempt to create an instance. But it seems like a very clunky and hacky way, causing more pain than join.

There's a deep blog on this subject, confirming my approach (as far I understand, at least). I only have one module (except the routing that doesn't relate to this) so this answer won't do. Also, here they give the solution to the exact opposite behavior requested, so I'm concluding that my way is the right one if I want the service to be instantiated once and once only but injected in multiple components.

3
  • for a simple test case remove every reference to the service, remove the 'providedIn' and added in your desired module (unless u are using it in your app component) and then finally inject the service inside your component's constructor Commented Dec 14, 2019 at 23:04
  • please include an example of how you are injecting the service Commented Dec 14, 2019 at 23:06
  • What ever you're doing, it's wrong. We can't tell, though, because the post doesn't have enough information to go on. I put together a quick project to try and repro this, and the service wasn't recreated during navigation. The singleton worked as expected. Give us a Minimal, Reproducible Example. Commented Dec 14, 2019 at 23:32

1 Answer 1

4

For the service to behave as a singleton, it shouldn't be added in any providers section. All you have to do is to import it in any component that its needed.

So just make sure it's not included in any providers.

Sign up to request clarification or add additional context in comments.

5 Comments

Liike I mentioned in the question, I've tried with it both added and commented out in the module file's providers section. I get the same behavior from the app.
Do a search in the project and find everywhere its referenced. Also what version of angular / angular cli are you using
Angular 8. There are two spots where I inject it into the constructors.
the last thing i can think of is to check the imports .. thats its only import {SecurityService} from './some/path without any extensions
If that checks out, try and recreate on stackblitz

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.