【问题标题】:Angular 6: You need to import the HttpClientModule in your AppModuleAngular 6:您需要在 AppModule 中导入 HttpClientModule
【发布时间】:2018-11-11 22:39:05
【问题描述】:

在减去之前,请注意没有报告的其他问题对我有帮助。浏览了有关此问题的整个 stackoverflow。

这是app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { LalaComponent } from './lala/lala.component';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { ApiModule } from 'atlas-client';

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

这是lala.component.ts:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-lala',
  templateUrl: './lala.component.html',
  styleUrls: ['./lala.component.css']
})
export class LalaComponent implements OnInit {

  constructor(private http: HttpClient) {
    console.log('test');
  }

  ngOnInit() {

  }
}

package.json:

{
  "name": "lala-app",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.3",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "core-js": "^2.5.4",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^6.0.3",
    "@angular-devkit/build-angular": "~0.6.6",
    "typescript": "~2.7.2",
    "@angular/cli": "~6.0.7",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  }
}

我收到此错误:

错误:您需要在 AppModule 中导入 HttpClientModule!看 还有https://github.com/angular/angular/issues/20575

at new ApiModule (atlas-client.js:216)
at _createClass (core.js:9264)
at _createProviderInstance$1 (core.js:9234)
at initNgModule (core.js:9170)
at new NgModuleRef_ (core.js:9899)
at createNgModuleRef (core.js:9888)
at Object.debugCreateNgModuleRef [as createNgModuleRef] (core.js:11719)
at NgModuleFactory_.push../node_modules/@angular/core/fesm5/core.js.NgModuleFactory_.create

(core.js:12421) 在 core.js:4720 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388)

请注意,当我删除带有相应导入的 import { HttpClientModule } from '@angular/common/http'; 行时,问题仍然存在。

【问题讨论】:

  • 你能展示AppModule的课程吗?
  • 请显示app.module.ts的全部内容。
  • @ConnorsFan,完成
  • 您能否在 StackBlitz 或类似的情况下重现此行为?
  • 可以加package.json吗?你还在用atlas-client.js吗?

标签: angular


【解决方案1】:

当 swagger-codegen 包使用npm link 链接或直接使用:npm install PATH_TO_GENERATED_PACKAGE/dist --save 安装时,angular-cli 中存在错误(请参阅:https://github.com/angular/angular-cli/issues/8284)。

问题似乎仅限于ng serve,而不是ng build --prod --base-href=...,但我还没有测试“ng build”。

您需要在 AppModule 中导入 HttpClientModule!”消息无效,但无论如何都会抛出错误。

此票中描述的(丑陋的)解决方法是使用 swagger-codegen-cli(或 openapi-generator-cli)生成代码,搜索并注释掉错误消息:

// if (!http) {
//     throw new Error('You need to import the HttpClientModule in your AppModule! \n' +
//     'See also https://github.com/angular/angular/issues/20575');
// }

下一步:运行npm install && npm run build 并在消费项目中安装包。

更简洁的解决方案是使用(私有)npm 注册表。


2018 年 14 月 7 日更新

进一步调查表明,上述workaround 并不能解决所有问题。 相反,我现在使用的是私有 npm 注册表 Verdaccio 并且没有问题了。

-阿杰

【讨论】:

  • 在 angular.json 的 "architect"/"build"/ "options" 下添加 "preserveSymlinks": true 也解决了包与 npm link 链接时的问题,
猜你喜欢
  • 1970-01-01
  • 2019-06-20
  • 2021-04-27
  • 2018-05-03
  • 2019-05-15
  • 2018-06-24
  • 1970-01-01
  • 2022-08-24
  • 2018-02-03
相关资源
最近更新 更多