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

时间:2020-03-15 04:11:45

标签: angular typescript

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

但我无法导入 HttpClientModule

这是来自 VS Code 的错误消息:

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';
}

请注意:我没有在其他任何地方使用 HttpClientModule 。我导入并执行ng serve -o并收到错误消息。请指出出了什么问题

5 个答案:

答案 0 :(得分:35)

我遇到了同样的问题,我只运行了以下命令,之后便正常工作了。

npm cache verify

如果没有,请尝试手动删除node_modules文件夹,然后使用npm install重新安装模块。

rm -rf node_modules
npm install

在npm> 6上,您还可以运行npm ci,这也将删除node_modules文件夹并在之后重新安装软件包。

答案 1 :(得分:27)

在添加新程序包之前停止服务,在添加新程序包之后重新启动服务。这样可以解决这个问题。

答案 2 :(得分:1)

首先停止并运行以下命令...

import { createApp } from "vue";
import App from "./App.vue";

import GlobalComponent from "./components/GlobalComponent.vue";

createApp(App)
    .component("global-component", GlobalComponent)
    .mount("#app");

安装后,使用命令构建并运行项目

npm cache verify
rm -rf node_modules
npm install

它对我有用并解决了问题...

答案 3 :(得分:0)

我有同样的问题。因此,我手动删除了node_modules,然后删除了npm install。之所以解决,是因为依赖关系损坏。

答案 4 :(得分:-1)

就我而言,关闭 VS Code,重新打开项目并执行“ng serve”。成功了。