0.0.0 • Published 8 years ago
@vendasta/vhttp v0.0.0
vHttp
vHttp is an Angular2 component for making HTTP calls.
Requirements
First, you will need to include the component into your application.
npm install @vendasta/fec-icon --save
Using vHttp (or a service which uses it) requires having HTTP providers in your application (app.ts) as such:
@Component({
...
directives: [
HTTP_PROVIDERS
]
})
bootstrap(AppComponent, [
HTTP_PROVIDERS
]);
Usage
The vHttp service is wrapped by other services to allow the services to make HTTP calls. To use it: import it, include it within your providers, and instantiate it in your constructor.
import {Injectable} from "@angular/core";
import {Observable} from "rxjs/Observable";
import {VHttpService} from "@vendasta/vhttp/vhttp.service";
import {ExampleItem} from "./exampleItem";
@Injectable()
export class ExampleService {
constructor(private vHttp:VHttpService) {}
getExampleItems(url: string) : Observable<ExampleItem[]> {
return this.vHttp.get(url);
}
}
You can then use the example service to make API calls that returns the data field of a JSON object returned by the API.
import {VHttpService} from "@vendasta/vhttp/vhttp.service";
import {ExampleService} from "./example.service";
...
@Component({
...
providers: [VHttpService, ExampleService]
)}
export class TestComponent {
constructor(private exampleService: ExampleService){}
...
getExampleItems(url) {
this.exampleService.getExampleItems(url).subscribe(
result => this.testItems = result,
error => this.errorMessage = <any>error;
)
}
}