Tôi đang cố gắng để truy cập vào một cấp dữ liệu JSON
nhưng sau khi thực hiện các thay đổi từ the documentation tôi nhận được lỗi saugóc 4 - Không thể tìm thấy tên 'HttpClient'
"Không thể tìm thấy tên 'HttpClient"
Tôi đã xem qua các hướng dẫn một vài lần nhưng tôi đang đấu tranh để tìm lý do tại sao tôi nhận được lỗi này.
Thành phần của tôi nơi tôi thực hiện yêu cầu Http.
rooms.parent.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
/.../
})
export class RoomParentComponent implements OnInit {
results: string[];
// Inject HttpClient into your component or service.
constructor(private http: HttpClient) {}
ngOnInit(): void {
// Make the HTTP request:
this.http.get(/.../).subscribe(data => {
this.results = data;
});
}
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {HttpClientModule} from '@angular/common/http';
import { AppComponent } from './app.component';
import { RoomParentComponent } from './rooms.parent.component';
@NgModule({
declarations: [
AppComponent,
RoomParentComponent
],
imports: [
BrowserModule,
HttpClientModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Làm thế nào để giải quyết lỗi trên và tiêm HttpClient vào constructor?
Chỉnh sửa: phiên bản góc của tôi là 4.3.2
phiên bản góc nào? –
Bạn nên di chuyển công cụ http của bạn vào một dịch vụ và sau đó tiêm dịch vụ thay vì HttpClient vào thành phần của bạn. – Akkusativobjekt