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 {}
8
  • 2
    Doesn't look like a problem with the code. Commented Oct 30, 2017 at 10:42
  • I would like to see this error in real example Commented Oct 30, 2017 at 10:49
  • Could it be a hierarchy problem? Try importing the @angular/.. components before the ./app.. components. Commented Oct 30, 2017 at 10:50
  • @Sajal it didn't help, sadly Commented Oct 30, 2017 at 10:52
  • There are no errors before importing HttpClient? Commented Oct 30, 2017 at 10:53

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

3 Comments

I was having the same problem,missing the @Injectable in an Angular 4 app.
I was getting the same error and it was the problem. Thank you.
You have saved my day, and possibly my life.
9

Turns out, I missed

"emitDecoratorMetadata": true

in tsconfig

fml...

2 Comments

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...
I've been debugging this for over an hour - thanks much!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.