无法在 Angular 10 中处理 HttpClient

Raj*_*ngh 1 httpclient angular angular10

当我尝试这样做时ng build,我收到一条错误消息

ERROR in node_modules/@angular/common/http/http.d.ts:81: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 HttpClient 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.

81 export declare class HttpClient {
Run Code Online (Sandbox Code Playgroud)

我尝试删除node_modules并重新安装它ng install。我什至尝试过,npm ci因为我在其他一些博客文章中看到这是一个解决方案,但没有一个对我有用。

这是我的输出 ng version

Angular CLI: 10.1.2
Node: 14.11.0
OS: darwin x64

Angular: 10.1.2
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Ivy Workspace: Yes

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1001.2
@angular-devkit/build-angular   0.1001.2
@angular-devkit/core            10.1.2
@angular-devkit/schematics      10.1.2
@schematics/angular             10.1.2
@schematics/update              0.1001.2
rxjs                            6.6.3
typescript                      4.0.3
Run Code Online (Sandbox Code Playgroud)

我的AppModule看起来像这样

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import {HttpClientModule} from '@angular/common/http'
import {HttpClient} from '@angular/common/http'

import { AppComponent } from './app.component';
import { SocketService } from "./socket.service";

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        FormsModule,
        HttpClient,
        HttpClientModule
    ],
    providers: [SocketService],
    bootstrap: [AppComponent]
})
export class AppModule { }

Run Code Online (Sandbox Code Playgroud)

bry*_*n60 5

HttpClient是一项服务,而不是要导入的模块,请将其从您的导入数组中删除。

imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule
],
Run Code Online (Sandbox Code Playgroud)