Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 Http v.s HTTP_PROVIDERS

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])?

like image 804
George Huang Avatar asked Mar 22 '26 07:03

George Huang


2 Answers

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.

like image 119
Isaac Aggrey Avatar answered Mar 25 '26 01:03

Isaac Aggrey


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.

like image 40
George Huang Avatar answered Mar 24 '26 23:03

George Huang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!