The http observable makes an http request for each subscriber. So the 3 to 4 http request means you must have multiple components subscribing at the same time. To share a single http request for multiple observers, you need something like the share operator.
export class FetchService {
data$: Observable<any>;
constructor(){
this.data$ = this._http.request(this._url)
.map((res:Response)=>res.json())
.catch(this.errorHandler)
.share();
}
getData(){
return this.data$;
}
}
Now the multiple observers will share the same observable.
This operator is a specialization of publish which creates a subscription when the number of observers goes from zero to one, then shares that subscription with all subsequent observers until the number of observers returns to zero, at which point the subscription is disposed.
As for your ERR_EMPTY_RESPONSE
issue. I've run into this when my proxied api call timesout.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…