Since there's no code to relate, I can only give you a generic answer.
Based on your comment.
You would create a service class for example CompanyService
@Injectable()
export class CompanyService {
constructor(private http: Http) { }
private url: string = 'http://yourapiurl/';
getCompanies(): Observable<ICompany[]> {
return (this.http.get(this.url + 'api/company')
.map((response: Response) => {
return <ICompany[]>response.json();
})
.publishReplay(1)
.refCount()
.catch(CompanyService.handleError)) as any;
}
private static handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
}
From there you can inject the service into your components depending where you would need them. For example you need to get the data on your HomeComponent, you would inject the service.
@Component({
selector: 'app-home',
templateUrl: 'home.component.html',
styleUrls: ['home.component.scss']
})
export class HomeComponent implements OnInit {
constructor(private companyService: CompanyService) {
}
companies: ICompany[];
ngOnInit(): void {
this.companyService.getCompanies()
.subscribe(
(companies: ICompany[]) => {
this.companies = companies;
});
}
}
Hope that helps...
UPD
Caching using .publishReplay(1).refCount();