L. Heider
L. Heider

Reputation: 1819

Can a service be disposed when it's only injected in one component?

I have a Service and a Component. In the Component's Module the Service is provided as providers: [RegisterStoreService].

When the constructor of the Component is called the first time the Service's constructor is called too. But every other time the Component gets "constructed" the Service's constructor wont be called.

Is that how it's supposed to be? And in case it is, is there a way to dispose this very instance of the Service whenever the onDestroy method of the Component gets called?

The actual aim is to fully reset the RegisterStore.

Upvotes: 0

Views: 429

Answers (1)

Akshay Rana
Akshay Rana

Reputation: 1485

If you provide Service in a module, it is a singleton Service, that means only a single instance is created and shared among all the components.

If you want a separate instance every time, remove it from module's providers array, and add it to the @component decorator's providers array.

Upvotes: 3

Related Questions