This package is deprecated in favor of the HttpInterceptor
interface. For more information visit here.
Helper methods for Angular2 HTTP
npm i ng-http -S
import { NgHttpConfig, AfterHookFunction, BeforeHookFunction } from 'ng-http';
import { Response, Request } from '@angular/http';
export class NgHttpConfigDetail implements NgHttpConfig {
baseUrl: string;
afterHook: AfterHookFunction;
errorHook: AfterHookFunction;
beforeHook: BeforeHookFunction;
constructor() {
this.baseUrl = 'http://api.geonames.org';
this.afterHook = (res: Response) => {
return res.json().geonames;
};
this.beforeHook = (req: Request) => {
//just logging
console.log(req);
return req;
}
this.errorHook = (req: Request) => {
return console.log('Error Occurred')
};
}
}
...
import { NgModule } from '@angular/core';
import { NgHttpModule } from 'ng-http';
import { ngHttpConfig } from './http.config';
...
@NgModule({
...
imports: [
NgHttpModule
],
...
providers: [
{
provide: 'ngHttpConfig',
useClass: NgHttpConfigDetail
}
]
...
})
...
import { Component } from '@angular/core';
import { HttpClient } from 'ng-http';
...
@Component({
...
})
export class FooComponent {
constructor(
private httpClient: HttpClient
){}
}
- baseUrl: string
- beforeHook: BeforeHookFunction
- afterHook: AfterHookFunction
- errorHook: AfterHookFunction
- get(url: string, RequestOptionsArgs?): Observable
- put(url: string, body: string, options? :RequestOptionsArgs): Observable
- post(url: string, body: string, options? :RequestOptionsArgs): Observable
- delete(url: string, RequestOptionsArgs?): Observable
- patch(url: string, body: string, options? :RequestOptionsArgs): Observable
$ npm i @angular/core @angular/http
$ npm i rxjs zone.js reflect-metadata core-js
$ npm i
npm run start