3

I read an article for how to inject Http service into an Angular2 application.

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],
....

I thought Http service already included in the HTTP_PROVIDERS. (as below, according to the document).

"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"

If that's the case, how come we still need to import Http in? Can we only do

 import {HTTP_PROVIDERS} from 'angular2/http';

On the other hand, be more specifically, can we change the component providers to providers: [Http]? Or in the bootstrap, can we do bootstrap(app, [Http])?

2 Answers 2

5

HTTP_PROVIDERS is deprecated.

e.g.: As mentioned in this comment, instead of this:

@NgModule({
  declarations: [AppComponent],

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

use this

@NgModule({
  declarations: [AppComponent],

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

Note: you import the module in imports in @NgModule, not the @Component.

Sign up to request clarification or add additional context in comments.

Comments

1

in Angular2, we can import not only services, but also directives and values (constants) from a module. We import those "TYPES" in order to fulfill the strong-type feature of TypeScript. So we can refer to it in our component class code later.

1 Comment

Thumbs down for saying this is related to strong typing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.