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.
