I'm trying to extent angular 2 http class to be able to handle global errors and set up headers for my secureHttp service. I found some solutions but it doesn't work with final release of Angular 2.
There is my code:
File: secureHttp.service.ts
import { Injectable } from '@angular/core';
import { Http, ConnectionBackend, Headers, RequestOptions, Response, RequestOptionsArgs} from '@angular/http';
@Injectable()
export class SecureHttpService extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
}
File: app.module.ts
import { BrowserModule, Title } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { routing } from './app.routes';
import { AppComponent } from './app.component';
import { HttpModule, Http, XHRBackend, RequestOptions } from '@angular/http';
import { CoreModule } from './core/core.module';
import {SecureHttpService} from './config/secure-http.service'
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
CoreModule,
routing,
HttpModule,
],
providers: [
{
provide: Http,
useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) => {
return new SecureHttpService(backend, defaultOptions);
},
deps: [ XHRBackend, RequestOptions]
}, Title, SecureHttpService],
bootstrap: [AppComponent],
})
export class AppModule { }
component.ts
constructor(private titleService: Title, private _secure: SecureHttpService) {}
ngOnInit() {
this.titleService.setTitle('Dashboard');
this._secure.get('http://api.example.local')
.map(res => res.json())
.subscribe(
data => console.log(data) ,
err => console.log(err),
() => console.log('Request Complete')
);
}
For now it returns me an error 'No provider for ConnectionBackend!'.
Thanks for help!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…