如何在app.module.ts中导入HttpClientModule而不出现StaticInjection错误?

问题描述

我正在尝试摆脱旧的http调用方式。我正在从4号角迁移到5号角。 在我的app.module.ts中,我做了如下必要的更改:

import { NgModule } from '@angular/core';
import { browserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
...................................................

@NgModule({
  imports: [
    browserModule,HttpClientModule,.................
  ],declarations: [
    AppComponent,.......................
  ],bootstrap: [ AppComponent ]
})
export class AppModule {} 

但是当我删除HTTP模块引用并导入HTTPClient模块引用时,我在下面得到了错误

   StaticInjectorError[Http]: 
  StaticInjectorError[Http]: 
    NullInjectorError: No provider for Http!
   Error: StaticInjectorError[Http]: 
  StaticInjectorError[Http]: 
    NullInjectorError: No provider for Http!


Unhandled Promise rejection: StaticInjectorError[Http]: 
  StaticInjectorError[Http]: 
    NullInjectorError: No provider for Http! ; Zone: <root> ; Task: Promise.then ; Value: Error: StaticInjectorError[Http]: 
  StaticInjectorError[Http]: 
    NullInjectorError: No provider for Http! Error: StaticInjectorError[Http]: 
  StaticInjectorError[Http]: 
    NullInjectorError: No provider for Http!
   at _NullInjector.prototype.get (http://localhost:4200/vendor.bundle.js:99248:13)
   at resolveNgModuleDep (http://localhost:4200/vendor.bundle.js:108909:5)
  

  NullInjectorError: No provider for Http!
   at _NullInjector.prototype.get (http://localhost:4200/vendor.bundle.js:99248:13)
   at resolvetoken (http://localhost:4200/vendor.bundle.js:99536:9)
   at tryResolvetoken (http://localhost:4200/vendor.bundle.js:99478:9)
   at StaticInjector.prototype.get (http://localhost:4200/vendor.bundle.js:99349:13)
   at _createProviderInstance$1 (http://localhost:4200/vendor.bundle.js:108920:13)",Symbol(observable)_h.ole4xhx75kj: undefined,Symbol(rxSubscriber)_g.ole4xhx75kj: undefined
   }
  

我也在使用rxjs版本:^ 5.5.2 我正在使用以下角度版本:

devDependencies:
"@angular/cli": "1.4.7","@angular/compiler-cli": "^5.0.0",------------------------------------------
dependencies:
"@angular/common": "^5.0.0","@angular/compiler": "^5.0.0","@angular/core": "^5.0.0","@angular/forms": "^5.0.0","@angular/http": "^5.0.0",

有人可以告诉我我要去哪里吗?

编辑:在整个应用程序中,所有适用的ts文件中的http的所有实例都已更改为httpClient。但是,一旦从HTTP Module删除app.module.ts并由HTTPClient Module取代了,我们就会看到上面问题中描述的问题。当前,当同时存在旧版HTTP模块和HTTPClient模块时,应用程序正在加载。如果其中任何一个删除,我将开始出错。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...