I have CustomHttp
class and I use it to add headers to my get
requests:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { RequestOptionsArgs, RequestOptions, ConnectionBackend, Http, Request, Response, Headers } from "@angular/http";
@Injectable()
export class CustomHttp extends Http {
headers: Headers = new Headers({ 'Something': 'Something' });
options1: RequestOptions = new RequestOptions({ headers: this.headers });
constructor(backend: ConnectionBackend,
defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
get(url: string, options?: RequestOptionsArgs) {
console.log('Custom get...');
return super.get(url, this.options1).catch(err => {
console.log(err);
if (err.status === 404) {
console.log('404 error');
return Observable.throw(err);
}
});
}
}
In RC5, I added it to my AppModule
providers like this:
provide (Http, {
useFactory: (
backend: XHRBackend,
defaultOptions: RequestOptions) =>
new CustomHttp(backend, defaultOptions),
deps: [XHRBackend, RequestOptions]
})
But, in RC6, provide
from @angular/core
is deprecated and I'm having problems adding my CustomHttp
class to AppModule
providers. Does anyone have an idea how to do this?
The syntax has change a bit, besides that it should still work the same:
{ provide: Http,
useFactory: (
backend: XHRBackend,
defaultOptions: RequestOptions) =>
new CustomHttp(backend, defaultOptions),
deps: [XHRBackend, RequestOptions]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With