我正在尝试从应用程序的后端获取数据。数据获取与承诺和获取函数的组合配合得很好。但当我尝试使用 HttpClient 服务实现它时,它
我正在尝试从应用程序的后端获取数据。数据获取与承诺和获取函数的组合配合得很好。但是当我尝试使用 HttpClient 服务实现它时,它给出了错误。我检查了这个平台中的先前响应,但无法解决问题。
应用程序.config.ts
import {ApplicationConfig} from '@angular/core';
import { provideHttpClient } from '@angular/common/http';
export const appConfig: ApplicationConfig = {
providers: [provideHttpClient()],
};
住房.服务.ts
export class HousingService {
url = "http://localhost:5150"
protected homeLocationList2: HomeLocation[] = [];
constructor(private http:HttpClient) {
}
getAllProduct(): Observable<HomeLocation[]>{
const response = this.http.get<HomeLocation[]>(`${this.url}/Home/AllProduct`)
return response
}
}
主页.Component.ts
export class HomeComponent {
productList:HomeLocation[] = [];
housingService: HousingService = inject(HousingService)
constructor(){
this.housingService.getAllProduct().subscribe(data=>{
this.productList = data;
})
}
}
我收到的错误
ERROR NullInjectorError: NullInjectorError: No provider for _HttpClient!
at NullInjector.get (core.mjs:1632:21)
at R3Injector.get (core.mjs:3014:27)
at R3Injector.get (core.mjs:3014:27)
at injectInjectorOnly (core.mjs:1095:36)
at Module.ɵɵinject (core.mjs:1101:40)
at Object.HousingService_Factory [as factory] (housing.service.ts:8:28)
at core.mjs:3132:35
at runInInjectorProfilerContext (core.mjs:866:5)
at R3Injector.hydrate (core.mjs:3131:11)
at R3Injector.get (core.mjs:3005:23)