这是我的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)
答案 0 :(得分:3)
您尚未在模块中提供提供程序;
import { HttpClientModule, HttpClient } from '@angular/common/http';
.
.
.
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
BrowserAnimationsModule,
FormsModule,
AppRoutingModule
],
providers: [ HttpClientModule, ... ]
希望这可以解决您的问题