angular2-services - Angular2 Http 与 HTTP_PROVIDERS

标签 angular2-services

我阅读了一篇关于如何将 Http 服务注入(inject) Angular2 应用程序的文章。

https://angular.io/docs/ts/latest/api/http/HTTP_PROVIDERS-let.html

import {HTTP_PROVIDERS, Http} from 'angular2/http';

@Component({
  selector: 'app',
  providers: [HTTP_PROVIDERS],
....

我以为Http 服务已经包含在HTTP_PROVIDERS 中了。 (如下,根据文档)。

"The providers included in HTTP_PROVIDERS include:

Http
XHRBackend
BrowserXHR - Private factory to create XMLHttpRequest instances
RequestOptions - Bound to BaseRequestOptions class
ResponseOptions - Bound to BaseResponseOptions class"

既然如此,那还需要导入Http吗?我们只能做吗

 import {HTTP_PROVIDERS} from 'angular2/http';

另一方面,更具体地说,我们能否将组件提供者更改为 提供者:[Http]? 或者在 Bootstrap 中,我们可以执行 bootstrap(app, [Http]) 吗?

最佳答案

HTTP_PROVIDERSdeprecated .

例如:如 this comment 中所述,而不是这个:

@NgModule({
  declarations: [AppComponent],

  providers:    [
    HTTP_PROVIDERS
  ],
   ...
})
export class AppModule { }

用这个

@NgModule({
  declarations: [AppComponent],

imports: [
    HttpModule
  ],
   ...
})
export class AppModule { }

注意:您在@NgModule 中的imports 中导入模块,而不是@Component

关于angular2-services - Angular2 Http 与 HTTP_PROVIDERS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36134238/

相关文章:

Angular 2兄弟组件2方式绑定(bind)

Angular 2 服务双向数据绑定(bind)

angular - 如何在 typescript 的泛型类中创建一个类型为 'T' 的新对象?

angular - 在 RxJs 5 中共享 Angular Http 网络调用结果的正确方法是什么?

javascript - 从多个组件更新服务数据 - Observable subscribe

javascript - 如何从 Datepicker 中的代码更改时间?

angularjs - angular 2 observable.subscribe 在第二次调用之前不会执行

javascript - Angular2 Click 方法总是返回 false 值

dart - Angular 2 Dart 中的网络 worker

angular - 通过适当的更改跟踪将服务属性绑定(bind)到组件属性