Another solution is below way and It was my fault that when happened I put HomeService
in declaration section in app.module.ts
whereas I should put HomeService
in Providers section that as you see below HomeService
in declaration:[]
is not in a correct place and HomeService
is in Providers :[]
section in a correct place that should be.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './components/home/home.component';
import { HomeService } from './components/home/home.service';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
HomeService // You will get error here
],
imports: [
BrowserModule,
BrowserAnimationsModule,
AppRoutingModule
],
providers: [
HomeService // Right place to set HomeService
],
bootstrap: [AppComponent]
})
export class AppModule { }
hope this help you.