Sachin
Sachin

Reputation: 475

Angular - NGRX signal stores - duplicate injections

In an NGRX signal store,

If I need access to a service inside both the withComputed and withMethod blocks, I currently have to inject the same service in each.

Is there a way of making the service accessible to the entire signal store without having to inject the service twice?

Upvotes: 1

Views: 552

Answers (2)

behruz
behruz

Reputation: 588

No, it is perfectly fine to inject the dependency for each block, as you are injecting the same instance, so no worries there.

However, injecting a service inside a withComputed block seems a little unusual. Instead, you could modify your store in a way that eliminates the dependency on the service for withComputed block (the service shouldn't handle state management anymore!). This way, in the withComputed block, you would only depend on the store values.

Upvotes: 0

Moh K
Moh K

Reputation: 26

One approach is to create a class property in the signal store class to hold the reference to the service. Then, use this property in both withComputed and withMethod blocks

Upvotes: 0

Related Questions