StaticInjectorError

时间:2019-01-14 13:57:12

标签: angular angular-httpclient

这是我的HttpClient代码:

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;
        });
    }
}

当我运行代码时,Chrome的控制台告诉我:

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

enter image description here

1 个答案:

答案 0 :(得分:3)

您尚未在模块中提供提供程序;

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

希望这可以解决您的问题