Angular 2 RC 5 Bootstrap 自定义 HTTP 类 [英] Angular 2 RC 5 Bootstrap custom HTTP class
问题描述
在 Angular 2 RC 4 中,我有一个 HttpLoading 类,它扩展了 Angular2 的原始 Http
In Angular 2 RC 4 I have a class HttpLoading which extends the original Http of Angular2
我可以使用以下代码在引导程序中使用它而没有任何问题:
I was able to use that in bootstrap without any issue using the below 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));
我的 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);
}
}
对于 RC 5,我不确定如何将这部分代码迁移到 NgModule 提供程序列表.
With RC 5 I am not sure how do I migrate this portion of the code to NgModule providers list.
我已遵循迁移指南并将我的 NgModule 更新为以下内容:
I have followed the migration guide and updated my NgModule to the following:
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 服务.
But it still seems that its not properly added because in my component when I use http its still hitting the default http instead of my custom http service.
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) => {
});
}
}
谁能指导一下?
推荐答案
非常感谢大家的帮助,我能够通过删除
Thank you so much everyone for your help, I was able to resolve the issue by removing
{ provide: RequestOptions, useClass: DefaultRequestOptions },
这对于 RC 5 来说似乎是不必要的.
Which seems to be unnecassary with RC 5.
这篇关于Angular 2 RC 5 Bootstrap 自定义 HTTP 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!