I'm trying to port an angular 2 application to RC5, but I have a problem: I cannot setup the application correctly via modules.
There's a service named CustomersService that retrieves customers data via Http. I've create an App Module that imports HttpModule and provides CustomersService. In another module, named HomeModule, there's a component that uses that service. When the application starts I see the following error on the console:
EXCEPTION: Error in ./HomeComponent class HomeComponent_Host - inline template:0:0
ORIGINAL EXCEPTION: No provider for Http!
I have no idea why this is happening...
Any suggestions will be appreciated.
Thank you.
Here are the files:
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { MdSidenavModule } from '@angular2-material/sidenav';
import { MdListModule } from '@angular2-material/list';
import { MdIconModule } from '@angular2-material/icon';
import { MdToolbarModule } from '@angular2-material/toolbar';
import { MdButtonModule } from '@angular2-material/button';
import { AppComponent } from './app.component';
import { routing } from './app.routing';
import { CustomersService } from './shared/rest_services/customers.service';
import { HomeModule } from './home/home.module';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
HttpModule,
// material design
MdSidenavModule,
MdListModule,
MdIconModule,
MdToolbarModule,
MdButtonModule,
routing,
// SharedModule.foorRoot(),
HomeModule
],
providers: [
CustomersService, Http
],
bootstrap: [AppComponent]
})
export class AppModule {
}
app.component.ts
import { Component } from '@angular/core';
import { CustomersService } from './shared/rest_services/customers.service';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent {
views = [ {title: 'Home', icon: 'home', path: 'home' } ];
constructor() {
}
}
customers.service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/Http';
@Injectable()
export class CustomersService {
private static _base = 'http://...';
constructor(private _http: Http) {
}
getAll() {
return this._http.get(CustomersService._base + "/customers.json");
}
}
home.module.ts
import { NgModule } from '@angular/core';
import { routing } from './home.routing';
import { HomeComponent } from './home.component';
@NgModule({
imports: [ routing ],
declarations: [
HomeComponent
],
exports: [
HomeComponent
]
})
export class HomeModule {
}
home.component.ts
import { Component, OnInit } from '@angular/core';
import { CustomersService } from '../shared/rest_services/customers.service';
@Component({
selector: 'app-home',
templateUrl: 'home.component.html',
styleUrls: ['home.component.scss']
})
export class HomeComponent implements OnInit {
constructor(private _customers: CustomersService) {
this._customers.getAll().subscribe((data) => {
console.log(data);
});
}
ngOnInit() {
}
}
I don't think that you need to specify something regarding Http in the providers
attribute of your module. This is done when including the HttpModule
in the imports
attribute.
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
HttpModule,
// material design
MdSidenavModule,
MdListModule,
MdIconModule,
MdToolbarModule,
MdButtonModule,
routing,
// SharedModule.foorRoot(),
HomeModule
],
providers: [
CustomersService // <-----
],
bootstrap: [AppComponent]
})
export class AppModule {
}
The HttpModule
configures the related providers for HTTP. See https://github.com/angular/angular/blob/master/modules/%40angular/http/http.ts#L354
Otherwise, perhaps it's a typo:
import { Http } from '@angular/Http';
instead of
import { Http } from '@angular/http';
import http, then declare constructor having parameter of type Http, like below
import {Http} from "@angular/http";
@Component({......})
export class CompName
{
......
constructor(private http: Http)
{
....
}
.....
}
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