下面是关于Http的this篇基础教程.

正如您可以在"设置:安装模块"一节中看到的那样,他们按如下方式导入HttpClientModule:

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

当我在我的项目中try 这一点时,我得到了以下错误:"找不到模块'@angular/common/http'".

我已经try 导入以下模块,如下所示:

import { HttpModule } from '@angular/http';

然后是我的进口区:

imports: [
    HttpModule
],

现在的问题是,我无法将此HttpModule注入到我的服务对象中,并收到以下错误:"无法找到模块HttpModule".

这是我的服务课:

import { Injectable, OnInit } from '@angular/core';
//Custom Models
import { Feed } from '../Models/Feed';

@Injectable()
export class FeedsService {
    constructor(private httpClient: HttpModule) {}
}

我做错了什么?

Update

推荐答案

Important:HttpClientModule代表Angular 4.3.0及以上.查看@Maximus'条 comments 和@Pengyy's answer了解更多信息.


Original answer:

您需要在组件/服务中注入HttpClient,而不是在模块中.如果您在@NgModule中导入HttpClientModule

// app.module.ts:
 
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
 
// Import HttpClientModule from @angular/common/http
import {HttpClientModule} from '@angular/common/http';
 
@NgModule({
  imports: [
    BrowserModule,
    // Include it under 'imports' in your application module
    // after BrowserModule.
    HttpClientModule,
  ],
})
export class MyAppModule {}

所以改变

construc到r(private httpClient: HttpModule) {}

construc到r(private httpClient: HttpClient) {}

正如documentation年前写的那样


但是,由于您导入了HttpModule

you need 到 inject construc到r(private httpClient: Http) as @Maximus stated in the comments and @Pengyy in this answer.

有关HttpModuleHttpClientModule之间差异的更多信息,请查看this answer

Angular相关问答推荐

Angular 形式验证:在空和不匹配的密码上显示错误

具有多重签名的Angular Mocking Service

如何使用解析器处理Angular 路由中存储的查询参数以避免任何额外的导航?

Angular 17不接受带点(.)的路径在开发环境中,它直接抛出一个404错误

Angular 17+独立配置中main.ts bootstrap 调用的最佳实践

如何使用来自不同可观测对象的值更新可观测对象

如果我只在组件中使用某个主题,是否需要取消订阅该主题?

VS 2022 停留在运行 Angular 应用程序上

nx workspace angular monorepo 在创建新应用程序时抛出错误

从具有特定 node 值的现有数组创建新数组

Angular APP_INITIALIZER 中的 MSAL 身份验证

如何在 form.value 中禁用复选框

Angular combinelatest使用没有初始值的主题

Angular 2 中的 ChangeDetectionStrategy.OnPush 和 Observable.subscribe

Angular 2+ 一次性绑定

Angular 7 测试:NullInjectorError:No provider for ActivatedRoute

Angular-CLI 代理到后端不起作用

在 Angular rxjs 中,我什么时候应该使用 `pipe` 与 `map`

Angular将 Select 值转换为 int

为什么 *ngIf 不适用于 ng-template?