Angular 4 HttpClient in Component Can't resolve all parameters
Asked Answered
F

3

5

I started learning Angular 4 and got to the part with HTTP Client. Right now I'm trying to make an http call from the component (yes, I know I should transfer it to service, but still)

But for some reason, when I try to inject HttpClient into my Component I get the next error:

Uncaught Error: Can't resolve all parameters for PromocodeComponent: (?).

Here's the code of my component:

import { Ticket } from '../../classes/Ticket.class'
import { Component, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'promocode',
  templateUrl: './promocode.template.html',
  styleUrls: ['./promocode.styles.scss']
})
export class PromocodeComponent {
  @Input() ticket: Ticket;
  state: String = "normal";
  promocode: String = "";

   constructor(private http: HttpClient) {}

  promocodeValidate(event): void{
    console.log(this.promocode);
    console.log(event);
    this.http.get('/promocode/asdasda').subscribe(data => {
      console.log(data);
    });
  }
}

And my app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { MovieBadgeComponent } from './movie-badge/movie-badge.component';
import { TicketComponent } from './ticket/ticket.component';
import { PromocodeComponent} from './promocode/promocode.component';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';


@NgModule({
  declarations: [
    AppComponent,
    MovieBadgeComponent,
    TicketComponent,
    PromocodeComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [ AppComponent ]
})
export class AppModule {}
Forde answered 30/10, 2017 at 10:39 Comment(8)
Doesn't look like a problem with the code.Aegir
I would like to see this error in real exampleSullins
Could it be a hierarchy problem? Try importing the @angular/.. components before the ./app.. components.Lydgate
@Lydgate it didn't help, sadlyForde
There are no errors before importing HttpClient?Lydgate
@Lydgate if I remove consturctor with HttpClient from the component, error disappearsForde
Could you please create a plnkr and replicate this error?Colorist
you shouldn't do this in your component, try setup this in your serviceAmargo
F
9

Turns out, I missed

"emitDecoratorMetadata": true

in tsconfig

fml...

Forde answered 30/10, 2017 at 15:38 Comment(2)
Thanks a lot, this saved my date. automatic DI always relies on outside magic such as this and thats why it has always been a pain...Swirsky
I've been debugging this for over an hour - thanks much!Bihar
K
18

It turned out, for me, that I forgot the @Injectable decorator on my service. In earlier angular (>=2), i remember the @Injectable as being optional... Guess not anymore?

Kerch answered 15/12, 2017 at 22:17 Comment(3)
I was having the same problem,missing the @Injectable in an Angular 4 app.Resolute
I was getting the same error and it was the problem. Thank you.Chronicles
You have saved my day, and possibly my life.Joliejoliet
F
9

Turns out, I missed

"emitDecoratorMetadata": true

in tsconfig

fml...

Forde answered 30/10, 2017 at 15:38 Comment(2)
Thanks a lot, this saved my date. automatic DI always relies on outside magic such as this and thats why it has always been a pain...Swirsky
I've been debugging this for over an hour - thanks much!Bihar
P
1

I got the same error when I ran unit testing (a spec.ts file) with Angular.

The following is what I did:

     import { HttpClientModule, HttpClient } from '@angular/common/http'; 

     beforeEach(async(() => {
       TestBed.configureTestingModule({
         declarations: [ ],
         imports: [
           HttpClientModule
         ],
         schemas: [NO_ERRORS_SCHEMA],
         providers: [
           HttpClient,
         ]
       })
         .compileComponents();
      }));

and the error was gone.

Pretorius answered 14/8, 2019 at 20:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.