在 Angular 2 RC 4 中,我有一个 HttpLoading 类,它扩展了 Angular 2 的原始 Http 服务。
使用 RC 4,我可以使用以下代码在 Bootstrap 中毫无问题地使用它:
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));
我的 DefaultRequest 选项类
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);
}
}
在 Angular 2 RC 5 中,bootstrap 的方式已经改变,我们现在需要 NgModule 和 bootstrap。我已按照迁移指南创建了以下 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]
},
GlobalService
],
bootstrap: [AppComponent],
})
export class AppModule { }
但是现在当我在我的组件中使用它时,它不会向服务器发送任何请求,并且会在控制台中出现以下错误:
EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot read property 'toString' of null
我使用 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) => {
});
}
}
谁能指导一下?
最佳答案
关于Angular 2 RC 5 Bootstrap 自定义 HTTP 类 TypeError : Cannot read property 'toString' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38896986/