sebholstein / tinystate

A tiny, yet powerful state management library for Angular

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Container dependency injection

bellizio opened this issue · comments

Is it possible to inject a dependency (i.e. a service) into a container?

For example, say I want to fetch some data asynchronously from an api using the HttpClient. Is that something this library supports or is it out scope?

Yes, you can, the container is just a class, it can be Injectable

@Buggytheclown I tried to, but was unsuccessful.

Here is a code snippet:

screenshot 2018-04-17 11 26 22

You have to call super() in the constructor since AppContainer extends Container. However, the Container class takes a _rootContainer param of type RootContainer. So I don't know what to pass into the super() call that would satisfy the _rootContainer param.

What I ended up doing instead was injecting the AppContainer into my service and updating the state from there. Probably a better solution anyway since it keeps the Container clean and simple.

@bellizio
constructor(@Optional() @Inject(RootContainer) rootContainer: | null){ super(rootContainer); }
API can be improved )

@bellizio Sorry for the delay. As @Buggytheclown explained, it's totally fine to inject your services or other stuff.

@Buggytheclown do you have an API in mind? I can't think of another solution. Maybe we should document it

@SebastianM it could be easier to pass Injector itself to super and let RootContainer decide what it want.

constructor(private injector:Injector) { super(injector) }

@Buggytheclown good idea. I will add that

@Buggytheclown @SebastianM just to clarify: what should the code look like in my AppContainer's constructor in order to properly call super()?