简体   繁体   English

将http定义为DI会给出EXCEPTION:未捕获(按承诺):错误:DI错误

[英]defining http as DI gives EXCEPTION: Uncaught (in promise): Error: DI Error

I'm trying to use private http: Http and it gives me error_handler.js:54 EXCEPTION: Uncaught (in promise): Error: DI Error 我正在尝试使用private http: Http ,它给了我error_handler.js:54 EXCEPTION: Uncaught (in promise): Error: DI Error

I'm using http in my component 我在组件中使用http

component.ts component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { Http} from '@angular/http';
import {Observable,Subscription} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { NouiFormatter } from 'ng2-nouislider';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
@Component({
  selector: 'app-char-maker',
  templateUrl: './char-maker.component.html',
  styleUrls: ['./char-maker.component.css']
})
export class CharMakerComponent implements OnInit {
  constructor(
    private formBuilder: FormBuilder,
    private http: Http)
  { 
    var obj;
  }
  ngOnInit() {

}
}

If I comment private http: Http in constructor the error goes away. 如果我在构造函数中注释private http: Http ,错误将消失。 What I'm missing here? 我在这里想念的是什么?

You need to add HttpModule under imports, 您需要在导入下添加HttpModule

@NgModule({
    imports: [
       HttpModule
]

For Angular v 4.3 对于Angular v 4.3

You can use the HttpClientModule . 您可以使用HttpClientModule Import it from 从导入

import {HttpClientModule} from '@angular/common/http';

and add this to your imports 并将其添加到您的导入中

 imports: [
 HttpClientModule,
  //... more code
  ]

For < v4.3 对于<v4.3

You can just use the HttpModule and import it from 您可以只使用HttpModule并将其从

import { HttpModule }    from '@angular/http'

and add it to your imports 并将其添加到您的导入中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM