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])?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With