I am asking this question for learning purposes. Here is my service:
export class HomeService {
private generalstatistics = new ReplaySubject<object>();
constructor(
private http: HttpClient
) {
this.data();
}
public get generalStatistics(): Observable<object> {
return this.generalstatistics.asObservable();
}
data() {
this.http.get<any>(`${environment.apiUrl}/home/`)
subscribe(data => {
this.generalstatistics.next(data);
});
}
}
Here I have my first subscription. Then somewhere in a component that needs that data I have the following:
constructor(private dataSvc: HomeService) {
this.getData();
}
getData() {
this.dataSvc.generalStatistics
.subscribe(data => {
this.source = data;
});
}
Which has my second subscription. So just for the learning purpose, is it possible to avoid two subscriptions?
Thanks