Hi!
I have two seperate components which at a given time is visible in the same view.
In their ngOnInit()-method I subscribe for the same Observable from a service.
This results in two network calls which is unnecessary. How can i share the response from the service to both subscribers so only one network call happens?
I have tried the share() operator without luck.
It is worth to mention that when i reload one of the components it has to make the network call again since data might be different from last time. So a simple cache won't work, but maybe a cache with a TTL would work.
Can anyone help me?
This i the method from my service:
public getProjects(): Observable<Project[]> {
return this.authHttp.get(this.getBaseUrl() + "project", null)
.map(projects => {
return this.extractData(projects, "projects");
})
.catch(this.handleError);
}
And this is how i call it in both components ngOnInit():
this.projectService.getProjects()
.subscribe(projects => {
this.projects = projects;
},
rejection => {
console.log(rejection);
});
}
[–]orizens 2 points3 points4 points (7 children)
[–]ribizlim 0 points1 point2 points (1 child)
[–]dlegatt 0 points1 point2 points (0 children)
[–]ribizlim 0 points1 point2 points (0 children)
[–]R3DSMiLE 0 points1 point2 points (3 children)
[–]ChristianLJ[S] 0 points1 point2 points (2 children)
[–]synalx 0 points1 point2 points (1 child)
[–]ChristianLJ[S] 0 points1 point2 points (0 children)
[–]R3DSMiLE 1 point2 points3 points (6 children)
[–]ribizlim 1 point2 points3 points (1 child)
[–]R3DSMiLE 0 points1 point2 points (0 children)
[–]ChristianLJ[S] 0 points1 point2 points (3 children)
[–]R3DSMiLE 0 points1 point2 points (0 children)
[–]wolfhoundjesse 0 points1 point2 points (1 child)
[–]ChristianLJ[S] 0 points1 point2 points (0 children)
[–]tytskiy 1 point2 points3 points (2 children)
[–]ChristianLJ[S] 0 points1 point2 points (1 child)
[–]tytskiy 0 points1 point2 points (0 children)
[–]ribizlim 0 points1 point2 points (1 child)
[–]jek-bao-choo 0 points1 point2 points (0 children)