将http定义为DI给出了EXCEPTION:Uncaught(在promise中):错误:DI错误

时间:2017-07-26 05:41:14

标签: angular dependency-injection

我试图使用private http: Http并且它给了我 error_handler.js:54 EXCEPTION: Uncaught (in promise): Error: DI Error

我在我的组件中使用http

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() {

}
}

如果我在构造函数中评论private http: Http,则错误消失。 我在这里失踪了什么?

2 个答案:

答案 0 :(得分:1)

您需要在导入

下添加 HttpModule
@NgModule({
    imports: [
       HttpModule
]

答案 1 :(得分:1)

对于Angular v 4.3

您可以使用HttpClientModule。从

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

并将其添加到您的导入

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

For< V4.3

您可以使用HttpModule并从

导入
import { HttpModule }    from '@angular/http'

并将其添加到您的导入