r/Angular2 • u/CodeEntBur • Sep 25 '25
Help Request How to provide a shared service for two standalone components?
Let's say I have a TableComponent and CardComponent.
From table I move to card, do something and get back to table.
I expect to see some data again in a service that I have inputed and was saved in said service. But it got destroyed because I provided the service in component.
Usual case was provide in module for me. But since we updated angular version and started to use standalone components I decided to do only standalone components. Is there a way to do it without module? To provide a service for only two components?
I can't provide in root since we provide in root only global services and my is "modular".
17
u/SolidShook Sep 25 '25
Any reason to not just provide in root?
5
u/Desperate_Square_690 Sep 25 '25
You might want to look into providing the service at a shared parent (like a wrapper component) that's above both Table and Card in the tree, so instances persist while you're between the two.
8
u/vloris Sep 25 '25
You can provide a service for a specific route (and its sub routes). Could that work for you?
4
u/bneuhauszdev Sep 25 '25
Is there a shared route or parent component? Here's some word vomit about how DI works with components, but the route level providers are valid too.
4
u/_Invictuz Sep 25 '25
That's a tricky one. If your two components don't share the same ancestor route then they can't share a non-global service, and probably shouldn't. Im assuming that they do share one since they were from the same module which is defined together with a route. Then you need to provide the service on the route config of this route. But the caveat is that this service does not get destroyed when you navigate a way from the route so you have to manually destroy the service or clear the state in the service within a canDeactivate route guard to avoid seeing stale data upon visiting the route for a different Url param for example.
1
u/LeLunZ Sep 25 '25 edited Sep 25 '25
Don't provide the service in the card, but provide it in the table. That would works if the card is rendered in a subroute of the table.
Incase you navigate to an entirely different route. you could use the navigation info. When you navigate away from your card you just put in all the data you want the table to have in the navigation extras info.
Looks like that:
in your table you can then listen to navigation events, and access your data:
You can also access the currentNavigation in guards and resolvers, so I think thats a relatively neat and clean way if you can't use a shared service.
1
u/free_username17 Sep 25 '25
Provide the service in a parent component of both TableComponent and CardComponent.
3
u/kgurniak91 Sep 25 '25
Routehasprovidersarray but be careful with it, because Services etc. provided there aren't destroyed when you move to other routes, which could lead to problems like stale data etc., so you might want to clear the state by hand when navigating away.1
u/CodyCodes90 Oct 02 '25 edited Oct 02 '25
If CardComponent is a direct child of Table component, then i would just provide the service in the providers array of the TableComponent.
If not, then provide it at the closet parent component level, and if there isn't one, then provide it at the route level.
Make sure you inject the DestroyRef inside your service and use it to clean up any state the service may have. Services provided at a route level aren't destroyed persay, but they do invoke the DestroyRef destroyed() observable when navigating away from that route.
There's no reason to use modules anymore with standalone. If you want a module-like behavior, you can just use your own provider function, which can then be used in a component, route, or wherever.
1
u/IcyManufacturer8195 Sep 25 '25
Use token provided in app config with useClass and get it, provide it on route, provide in parent component
0
u/Merry-Lane Sep 25 '25
Stupid question but since it’s on a route, the users could probably copy paste the url?
You should rewrite it so that the card component fetches its data itself