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

今天很棒

烦恼之前,请注意,没有其他报告的问题对我有所帮助。浏览了有关此问题的整个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),位于zone.elegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invoke(zone.js:388)的core.js:4720

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

用户名

当swagger-codegen软件包使用链接npm link或直接使用安装时,在angular-cli中存在一个错误:(npm install PATH_TO_GENERATED_PACKAGE/dist --save请参阅: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注册表。


更新14/7/2018

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

-Arjen

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章