Angular:HttpClientModule 导入错误(无法解析为 NgModule 类)

标签 angular typescript

我试图在假 json 服务器上构建一个简单的应用程序

但我无法导入 HttpClient模块

这是来自 的错误消息VS 代码 :

ERROR in node_modules/@angular/common/http/http.d.ts:2801:22 - error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class.

    This likely means that the library (@angular/common/http) which declares HttpClientModule has not been processed correctly by ngcc, or is not compatible with Angular Ivy. Check if a newer version of the library is available, and update if so. Also consider checking with the library's authors to see if the library is expected to be compatible with Ivy.

    2801 export declare class HttpClientModule {

这里是 app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { EmployeeCreateComponent } from './employee-create/employee-create.component';
import { EmployeeEditComponent } from './employee-edit/employee-edit.component';
import { EmployeeListComponent } from './employee-list/employee-list.component';

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


@NgModule({
  declarations: [
    AppComponent,
    EmployeeCreateComponent,
    EmployeeEditComponent,
    EmployeeListComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'restTest6';
}

请注意:我没有使用 HttpClient模块其他任何地方。我导入并做了ng serve -o并得到错误。请指出哪里出了问题

最佳答案

我遇到了同样的问题,我只是运行以下命令,它在没有任何错误的情况下运行。

npm cache verify
如果没有,请尝试手动删除 node_modules文件夹并使用 npm install 重新安装模块.
rm -rf node_modules
npm install
在 npm > 6 时,您还可以运行 npm ci ,这也会删除 node_modules文件夹并在之后重新安装软件包。
编辑 正如其他答案所建议的,您可能需要重新启动 ng serve如果上述步骤不起作用(但它们通常会起作用)

关于Angular:HttpClientModule 导入错误(无法解析为 NgModule 类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60689668/

相关文章:

typescript - 如何在 typescript 中初始化 Passport 配置文件?

html - Angular 2 : How do you pass a selected item from a HTML datalist element back to the component?

angular - Jest : mock RxJs pipe

javascript - 使用装饰器中的原型(prototype)/访问基类属性访问派生类中的基类属性

javascript - 当条目存在时,删除 Typescript 接口(interface)列表中的元素,始终显示负索引

Typescript:无效的 Object.assign 类型?

angular - 类型 'controls' 上不存在属性 'AbstractControl' - angular4

来自另一个组件的 Angular 9 调用函数

Angular -> tslint 错误,规则定义 jsdoc/newline-after-description

typescript - Vue 增强类型 - XYZ 在类型上不存在