-2

This is my HttpClient code:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { CreeperUser } from '../Models/CreeperUser';
import { Observable } from 'rxjs';

@Component({
    templateUrl: 'friends.component.html'
})
export class FriendsComponent {
    users: number;

    constructor(private http: HttpClient) {
        this.DisplayUser();
    } 

    public CallApi_HttpGet(): Observable<CreeperUser[]> {
        return this.http.get<CreeperUser[]> ('https://localhost:44399/api/user');
    }

    public DisplayUser(){
        this.CallApi_HttpGet().subscribe(response => {
            this.users = response[0].userId;
        });
    }
}

And when I run the code, the Chrome's console told me this:

Error: Uncaught (in promise): Error: StaticInjectorError(AppModule) 

enter image description here

Dale K
  • 25,246
  • 15
  • 42
  • 71
Jeb Feng
  • 416
  • 4
  • 12
  • Note that a component shouldn't use the `HttpClient` directly, write services to manage that. But do you have the `HttpClientModule` anywhere? – jonrsharpe Jan 14 '19 at 13:59
  • Hey Play_D, your question could be improved : https://stackoverflow.com/help/how-to-ask. – Adrien H Jan 14 '19 at 14:01
  • 2
    Possible duplicate of [No provider for HttpClient](https://stackoverflow.com/questions/47236963/no-provider-for-httpclient) – Amit Chigadani Jan 14 '19 at 14:02
  • Please avoid putting image of error messages (or code) in questions, for various reasons. Actually, I can see that the most relevant and important message in the screnshot is "No provider for HttpClient" – Pac0 Jan 14 '19 at 14:04

1 Answers1

4

You have not provided providers in your module;

 import { HttpClientModule, HttpClient } from '@angular/common/http';
.
.
.
@NgModule({
imports: [
    BrowserModule,
    HttpClientModule,
    BrowserAnimationsModule,
    FormsModule,
    AppRoutingModule
],
providers: [ HttpClientModule, ... ]

Hope this will solve your problem

Sneha Pawar
  • 1,097
  • 6
  • 14