在angular2 rc 4中,我有一个类httploading,它扩展了angular2的原始http
我可以在引导程序中使用它,使用以下代码没有任何问题:

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));

我的默认请求选项类
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'
    });
}

我的httploading类如下所示:
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);
    }
}

对于rc 5,我不确定如何将这部分代码迁移到ngmodule providers列表。
我已经遵循迁移指南并将我的ngmodule更新为以下内容:
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 { }

但它似乎仍然没有正确添加,因为在我的组件中,当我使用http时,它仍然会命中默认http而不是自定义http服务。
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) => {

            });
    }
}

有人能指点一下吗?

最佳答案

既然>=RC5现在引导的是一个@NgModule类,而不是像这样引导根组件,那么请阅读this RC4 => RC5 transition guide的更多内容。

import { NgModule }       from '@angular/core';
import { HttpModule } from '@angular/http';
import { AppComponent }   from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [HttpModule],  // no need for HTTP_DIRECTIVES
  providers: [
    {provide: RequestOptions, useClass: DefaultRequestOptions },
    {provide: Http, useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) => new HttpLoading(backend, defaultOptions), deps: [XHRBackend, RequestOptions]}
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

platformBrowserDynamic().bootstrapModule(AppModule);

07-24 20:26