Access ElementInjector hierarchy from a resolver

Is it possible to access the instance of a service included on a component decorator providers array from out of its scope? The scope is basically all the children components of the component where it’s created, but I’d need to access it from a resolver.

I guess there should be some way to get it using the injector, but I’m not sure if the injector has access to the ElementInjector hierarchy.

Hi Pizzicato,
Is it possible to recreate your problem in some online editor so that we can understand your case clearly?

Hi @Pizzicato

Which service do you need?

Hi! Thanks for answering, guys!
Recreating the problem online would be much more difficult that just explaining it, because there are a lot of elements involved. I’ll try:
I have some services in my app which have data that I’d like to reset when the user logs out.

I have a parent component that is only shown when the user is logged in so if I include those services in the providers array of that component decorator, every time the user logs out, this component is destroyed, and the services instances are deleted as well. If another user logs in, this parent component would be created again with new services instances, thus fulfilling my needs.

This works well, but if I use a resolver that needs data from one (or more) of those services instances, how would I inject them in the resolver? Maybe the idea that I had on resetting those services data is not well thought, and should use an alternative (like a subject the services subscribe to, and emits on logout), but I find it really clean and seamless, since when I need to include another service that has data that needs to be reset on logout I would only have to add it to the parent component providers array.

Thanks!

Hi @Pizzicato,

I am not sure you can do it.
As far as I know the component instance is created only after the resolver resolves.
I haven’t checked it, but that’s what I remember from the docs.

Hey @Pizzicato, why do you want to tear down those services? To pass data from a resolver to a component, put it in the route data and access that from the component. Sounds like you have too many concerns in a single service. Split up inti a data fetching service that you use in the resolver and a stateful service that you provide and use in the component. The stateful service gets its state from route data which is hydrated by the resolver before the service is created.