【问题标题】:Angular2 Http v.s HTTP_PROVIDERSAngular2 Http 与 HTTP_PROVIDERS
【发布时间】:2016-03-21 14:48:44
【问题描述】:

我阅读了一篇关于如何将 Http 服务注入 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';

另一方面,更具体地说,我们可以将组件提供程序更改为 providers: [Http]? 或者在引导程序中,我们可以做bootstrap(app, [Http]) 吗?

【问题讨论】:

    标签: angular2-services


    【解决方案1】:

    HTTP_PROVIDERSdeprecated

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

    @NgModule({
      declarations: [AppComponent],
    
      providers:    [
        HTTP_PROVIDERS
      ],
       ...
    })
    export class AppModule { }
    

    使用这个

    @NgModule({
      declarations: [AppComponent],
    
    imports: [
        HttpModule
      ],
       ...
    })
    export class AppModule { }
    

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

    【讨论】:

      【解决方案2】:

      在 Angular2 中,我们不仅可以导入服务,还可以从模块中导入指令和值(常量)。我们导入这些“TYPES”是为了实现 TypeScript 的强类型特性。所以我们以后可以在我们的组件类代码中引用它。

      【讨论】:

      • 表示这与强类型有关。
      猜你喜欢
      • 2017-01-22
      • 1970-01-01
      • 2016-03-27
      • 2017-07-05
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      相关资源
      最近更新 更多