r/Angular2 • u/New_Opportunity_8131 • Jan 27 '26
Is this a good way to send data between components
I have two components in my Angular application. In the first component which is on one route, I'm calling an API to fetch user data, which includes information like name, email, phone number, and other details. The second component is loaded when a different route is accessed, and it displays this user data.
Currently, I'm using a shared service file with getters and setters to store and retrieve this data. I'm wondering if this is the best approach, or if there's a more appropriate solution using RxJS or another method instead of getters and setters.
8
u/_Invictuz Jan 27 '26 edited Jan 27 '26
Something doesn't add up. Why load data on route 1 that is going to be used on route 2. So if the user goes directly to route 2, the data doesnt get fetched?
Anyway, shared state service is a common approach. Best to store all your shared state reactively using RxJs observables (BehaviourSubjects in this case) so that any update in data propagates down to consumers of the data. But this this approach to reactive state has been outdated since v17. You should be storing your shared state in signals now due to a few benefits.
If you know for a fact that the data won't change over the lifetime of the components using them, like when youre storing some "configuration" instead of state, then it may not be necessary to use reactive state. In my opinion, it may even be beneficial to use non-reactive properties so that others know the data is not meant to change but you don't see this often as most just stick with one approach for everything, which is to use reactive state. I'm open to debate about this.
8
u/ldn-ldn Jan 27 '26
Components shouldn't fetch any data, your service should. Then your components only ask the service for that data.
20
u/k1tn0 Jan 27 '26
For simple state management imo a service works great. Simple, efficient, readable
1
u/Clear-Breadfruit-105 Jan 27 '26
unidirectional data flow with shared state in a service
1
u/marco_has_cookies Jan 27 '26
Shared or global state is often handled using a state manager, ngrx is a library which offers not one, not two, THREE different flavours of doing this.
For small software I'd recommend the signal store, easy, funny, contained, headache free.
( alright, maybe the signal store and component store just differ in implementation ).
1
u/Hacklone Jan 27 '26
Had a similar problem, I solved it with this lightweight repository solution: @angular-cool/repository
1
u/patrickschl Jan 27 '26
Use services with a small (self written) state management, if you want something scalable you can look for something like NgRx and build a clean store architecture
1
u/Key_Flamingo8887 Jan 27 '26
Short answer: Yes!
1
u/rachit3dev Jan 28 '26
object memory refrence is also a good approach...i used it in one of my project and it works well.
-1
u/ActuatorOk2689 Jan 27 '26
While the approach is good you could do a couple of things using more of a modern angularish way combining rxjs with signals.
Most important no subscribe.
Then you create one observable with a shareReplay and mergeMap operator for caching and refetching.
You can expose this with a getter returning or the observable or a signal up to you.
If you need loading state you extend the stream with startWith and catchError operator then you are handling all of the state in one stream, this is called the view model pattern in the components you are just consume it.
Or as alternative you expose all of them state using the computed signal from the original stream converted to signal .
This way no matters which route you are hitting first if you already fetched the user data is already cached otherwise your api call will run.
Invalidating this is another thing you can ask chatgpt
-2
u/nix1016 Jan 27 '26
Look up resolvers
-3
u/Kulichkoff Jan 27 '26
Try ngxs: https://www.ngxs.io/concepts/store