Angular 4 HttpClient in component Unable to resolve all parameters

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

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

Fault: unable to resolve all parameters for PromocodeComponent: (?).

Here is 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 {}
+5
source share
4 answers

Turns out I missed

"emitDecoratorMetadata": true

in tsconfig

FML ...

+5
source

, @Injectable . angular ( >= 2) @Injectable ... , ?

+12

You might need to update your development environment:

  • node
  • angular cli
  • angular version

and try again

0
source

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

Here 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 has disappeared.

0
source

Source: https://habr.com/ru/post/1688393/


All Articles