Angular 6:您需要在AppModule中导入HttpClientModule

时间:2018-06-01 18:39:15

标签: angular

在迷你之前,请注意,没有其他问题报告对我有帮助。查看有关此问题的整个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)       at core.js:4720       在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invoke   (zone.js:388)

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

1 个答案:

答案 0 :(得分:1)

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

问题似乎仅与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注册表。


更新14/7/2018

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

-Arjen