gpt4 book ai didi

Angular 4 - 找不到名称 'HttpClient'

转载 作者:太空狗 更新时间:2023-10-29 16:56:57 34 4
gpt4 key购买 nike

我正在尝试访问 JSON 提要,但在对 the documentation 进行了更改之后我收到以下错误

"Cannot find name 'HttpClient'"

我已经仔细阅读了教程几次,但我一直在努力寻找为什么会出现此错误。

我执行 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 { }

如何解决上述错误并将 HttpClient 注入(inject)构造函数?

编辑:我的 Angular 版本是 4.3.2

最佳答案

HttpClient 在 Angular 4.3.0 版本中引入。此外,您还必须确保在主 AppModuleimports 元数据中导入并注入(inject)了 HttpClientModule

// Import HttpClientModule from @angular/common/http in AppModule
import {HttpClientModule} from '@angular/common/http';


//in place where you wanted to use `HttpClient`
import { HttpClient } from '@angular/common/http';

关于Angular 4 - 找不到名称 'HttpClient',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45375476/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com