Use services to share data
Angular distinguishes components from services in order to increase modularity and reusability.
and It's Good Practice to Delegate complex component logic to services
From Angular Style Guide
Do limit logic in a component to only
that required for the view. All other logic should be delegated to
services.
Do move reusable logic to services and keep components simple and
focused on their intended purpose.
Why? Logic may be reused by multiple components when placed within a
service and exposed via a function.
Why? Logic in a service can more easily be isolated in a unit test,
while the calling logic in the component can be easily mocked.
Why? Removes dependencies and hides implementation details from the
component.
Why? Keeps the component slim, trim, and focused.
If your objective is to multicast the data use RXJS's Subject or BehaviorSubject
Subject acts as a bridge/proxy between the source Observable and many observers, making it possible for multiple observers to share the same Observable execution.
Advantages of BehaviorSubject over Subject
- It will always return the current value on subscription - there is no need to call
onnext().
- It has a
getValue() function to extract the last value as raw data.
- It ensures that the component always receives the most recent data.
- you can get an observable from behavior subject using the
asobservable()
method on BehaviorSubject.
Subject vs BehaviorSubject
Service
private firstResponse=new BehaviorSubject<any>('');
private secondResponse=new BehaviorSubject<any>('');
CurrentDatafirst = this.firstResponse.asObservable();
CurrentDatasecond = this.secondResponse.asObservable();
getdata(){
forkJoin([playerObservable, worldObservable])
.subscribe(([player, world]) => {
this.firstResponse.next(player),
this.secondResponse.next(world)
})
});
}
Component1:
ngOnInit()
{
this.service.CurrentDatafirst.subscribe(//value=>your logic);
this.service.CurrentDatasecond.subscribe(//value=>your logic)
}
Component2:
ngOnInit()
{
this.service.CurrentDatafirst.subscribe(//value=>your logic);
this.service.CurrentDatasecond.subscribe(//value=>your logic)
}
RxJS Subjects for human beings
BehaviorSubject in Angular
Live Demo
-------------------------------------------------------------------------------------
you could also share a single http request for multiple observers using shareReplay operator and take action accordingly.
You must be aware of the fact that http returns a cold observable and
When a cold observable has multiple subscribers, the whole data stream is re-emitted for each subscriber. Each subscriber becomes independent and gets its own stream of data
To Avoid Duplication of HTTP Requests shareReplay Operator is used.
Service
public response$:Observable<any>
getdata(){
forkJoin([playerObservable, worldObservable]).pipe(sharReplay(1));
}
fetchdata()
{
this.response$;
}
Component1:
ngOnInit()
{
this.service.fetchdata().subscribe(//value=>your logic);
}
Component2:
ngOnInit()
{
this.service.fetchdata().subscribe(//value=>your logic);
}
Live Demo