During my experience with Angular 2 RC 4, I encountered a situation where I needed to create a class called HttpLoading that extended the original Http class of Angular2.
I managed to integrate this successfully into my project using the following bootstrap code:
bootstrap(AppComponent, [
HTTP_PROVIDERS,
provide(RequestOptions, { useClass: DefaultRequestOptions }),
provide(Http, {
useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) => new HttpLoading(backend, defaultOptions),
deps: [XHRBackend, RequestOptions]
})
]).catch(err => console.error(err));
This is how my DefaultRequestOptions class looks like:
import { Injectable } from '@angular/core';
import { Headers, BaseRequestOptions } from '@angular/http';
@Injectable()
export class DefaultRequestOptions extends BaseRequestOptions {
headers: Headers = new Headers({
'Content-Type': 'application/json'
});
}
Here is the structure of my HttpLoading Class:
import { Http, RequestOptionsArgs, ConnectionBackend, RequestOptions, Response } from '@angular/http'
import { Injectable } from '@angular/core'
import {GlobalService} from './globalService';
import { Observable } from 'rxjs/Observable';
export class HttpLoading extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _globalService: GlobalService) {
super(backend, defaultOptions);
}
get(url: string, options?: RequestOptionsArgs): Observable<any> {
this._globalService.isLoading = true;
return super.get(url, options)
.map(res => {
this._globalService.isLoading = false;
return res.json();
})
.catch(this.handleError);
}
}
However, with RC 5, I am facing challenges in migrating this code snippet to the NgModule providers list.
After referencing the migration guide, I updated my NgModule as follows:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { Http, HTTP_PROVIDERS, RequestOptions, XHRBackend } from '@angular/http';
import { DefaultRequestOptions } from './DefaultRequestOptions';
import { HttpLoading } from './http-loading';
import { routing } from './app.routing';
import { AppComponent } from './components/app.component';
@NgModule({
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
HttpModule,
routing
],
declarations: [
AppComponent
],
providers: [
HTTP_PROVIDERS,
{ provide: RequestOptions, useClass: DefaultRequestOptions },
{
provide: Http,
useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, globalService: GlobalService) => new HttpLoading(backend, defaultOptions, globalService),
deps: [XHRBackend, RequestOptions, GlobalService]
}
],
bootstrap: [AppComponent],
})
export class AppModule { }
Unfortunately, it seems like the custom Http service is not being properly added because when I use http in my component, it still defaults to the standard http implementation instead of my customized one.
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
@Component({
templateUrl: '../../templates/home/home.html'
})
export class HomeComponent implements OnInit {
constructor(private http: Http) {}
ngOnInit() {
this.http.get('/home')
.subscribe((data) => {
});
}
}
If anyone can provide guidance on resolving this issue, it would be greatly appreciated. Thank you!