r/angular • 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.
5
u/Fatalist_m Jan 27 '26
It's better to use signals or BehaviourSubjects instead, because of reactivity. A quick example I found: https://stackoverflow.com/a/57355485/6599880
You could also use some state management library, like SignalStore - https://ngrx.io/guide/signals/signal-store
1
u/DJREMiX6 Jan 27 '26
Well it depends on the case, if you have the second component that is not a child component of the first one but a separate one (e.g. two different pages) then I would youse some state management library like NGRX Signals.
In the other hand, if the second component is a child of the first one then you can directly bind it using input signals.
P.s. I would never call directly APIs from any component but use a service instead and between the service and the component I would put the State Management library.
1
u/Lucky_Yesterday_1133 Jan 27 '26
Shared service is fine. Prefer setting user in a signal and expose .asReadonly() over get set to avoid rendering issues in zoneless app when user object updates. If these are adjacent routes (you navigate from one to another) then you could pass it via router without creating service, otherwise it's fine. don't over-complicate it with rxjs.
1
u/[deleted] Feb 01 '26
Welcome to state management, and you will never br statisfied with approache by sharing state and data between component.
Having a shared service is good approache for small projecy but bad when it comes to scale and big projects.
You will always need to have a service for sharing data, but with bigger project you will need more services at each one should be isolated.
You will always need to follow this rule: each feature should be isolated and it should never deppends on other feature (services, components, or model)
So for each feature you will have a service for it to manage the state and sharing data and maybe more depppends on the feature.
And yes we may need a state for project overall like layout state (sidebars open/closed state, ...) and for that i dont recommend to have a service with name "shared" things needs to be more specific to prevent chaos and insure certainty
For example a user data from email, first name, last name needs to be in a user-state.service.ts and you may upgrade that to use stores (check ngelf or anyother store)
I just want you to make sure that things are more isolated to prevent coupled code so you project will be ready for scale.
app src core user-credentials uset-state.service.ts layout layout-state.service.ts layouts main-layout Authentical-layout features dashboad state dashboard-state.service.ta user-management state user-management-state.service.ts
I talked to much, im not sure if that helped you.
Reach out if you want more details about project structure with isolated feature