Common Task: I want to fetch data via HTTP only once and then make it accessible to my whole app as properties. But of course my app needs to "listen" to the properties, because HTTP is slow. I only want to call HTTP once (1 time) overall in the entire app.
@Injectable()
export class EnvService {
imageTag: String;
envData: Observable<any>;
constructor(protected authHttp: AuthHttp) {
let envSubscription = this.getConfig().subscribe(envConfig => {
this.imageTag = envConfig.imageTag;
this.envData = {
'imageTag' : envConfig.imageTag,
}
envSubscription.unsubscribe();
});
}
getData(): Observable<any> {
return this.envData;
}
getConfig(): Observable<any> {
const requestOptionsArgs: RequestOptionsArgs = {
url: '/assets/env/env.json',
method: RequestMethod.Get
};
const requestOptions: RequestOptions = new RequestOptions(requestOptionsArgs);
return this.authHttp.request(new Request(requestOptions)).map(returnedEnvConfig => {
return {
imageTag: returnedEnvConfig.json().imageTag,
};
});
}
}
imageTag: any is not assignable to type Observable
Any ideas how to do this?