No provider for simple component

Алекса Јевтић picture Алекса Јевтић · Nov 27, 2017 · Viewed 14.7k times · Source

Makes no sense it was working until 5 mins ago ... this is the component

import { Component, OnInit } from '@angular/core';
import {Osobaa} from '../osobaa';
import { Osoba } from '../osoba';
import {OsobaService} from '../osoba.service';

@Component({
  selector: 'app-form-add',
  templateUrl: './form-add.component.html',
  styleUrls: ['./form-add.component.css']
})
export class FormAddComponent implements OnInit {
  osoba : Osobaa;
  constructor() { }

  ngOnInit() { }

}

and this is the main app.module.ts

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import {FormsModule} from "@angular/forms";

import { AppComponent } from "./app.component";
import { HeaderComponent } from "./header/header.component";
import { LoginFormComponent } from "./login-form/login-form.component";
import { FooterComponent } from "./footer/footer.component";
import { DashboardComponent } from "./dashboard/dashboard.component";
import { RouterModule, Routes } from "@angular/router";
import {UserService} from './user.service';
import {AuthguardGuard} from './authguard.guard';
import { UserComponent } from "./user/user.component";
import { NotfoundComponent } from './notfound/notfound.component';
import { TableViewComponent } from './table-view/table-view.component';
import {HttpClientModule} from '@angular/common/http';
import {OsobaService} from './osoba.service';
import { HttpModule } from '@angular/http';
import { FormAddComponent } from './form-add/form-add.component';



const appRoutes: Routes = [
  {
    path: "",
    component: LoginFormComponent
  },
  {
    path: "add" , 
    component: FormAddComponent
  },
  {
  path: "table", component : TableViewComponent
  },
{
  path: "users/:name", 
  component : UserComponent
},
  {
    path: "dashboard",
    canActivate:[AuthguardGuard],
    component: DashboardComponent
  },
  {
    path: '**',
    component: NotfoundComponent
  }
];

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    LoginFormComponent,
    FooterComponent,
    DashboardComponent,
    UserComponent,
    NotfoundComponent,
    TableViewComponent,
    FormAddComponent
  ],
  imports: [BrowserModule, RouterModule.forRoot(appRoutes),HttpModule,FormsModule],
  providers: [UserService,AuthguardGuard,HttpClientModule,OsobaService],
  bootstrap: [AppComponent]
})
export class AppModule {}

and the console error is :

ERROR Error: Uncaught (in promise): Error: StaticInjectorError[FormAddComponent]: StaticInjectorError[FormAddComponent]: NullInjectorError: No provider for FormAddComponent! Error: StaticInjectorError[FormAddComponent]: StaticInjectorError[FormAddComponent]: NullInjectorError: No provider for FormAddComponent!

Answer

Pankaj Parkar picture Pankaj Parkar · Nov 27, 2017

Remove HttpClientModule module from providers, it supposed to be put inside imports option of NgModule. Also try re-build / re-run application.

Probably you are trying to inject Component into Service as injectable.