我得到的例外:没有提供Http!我做错了什么?

import {Http, Headers} from 'angular2/http';
import {Injectable} from 'angular2/core'


@Component({
    selector: 'greetings-ac-app2',
    providers: [],
    templateUrl: 'app/greetings-ac2.html',
    directives: [NgFor, NgModel, NgIf, FORM_DIRECTIVES],
    pipes: []
})
export class GreetingsAcApp2 {
    private str:any;

    constructor(http: Http) {

        this.str = {str:'test'};

        http.post('http://localhost:18937/account/registeruiduser/',
            JSON.stringify(this.str),
            {
                headers: new Headers({
                    'Content-Type': 'application/json'
                })
            });

当前回答

在使用它之前,将HttpModule添加到app.module.ts文件中的imports数组中。

从@angular/http导入{HttpModule}; @NgModule ({ 声明:[ AppComponent, CarsComponent ], 进口:[ BrowserModule, step 4 ], 供应商:[], 引导(AppComponent): }) 导出类AppModule {}

其他回答

>= Angular 4.3

用于引入的HttpClientModule

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

@NgModule({
  imports: [
    BrowserModule,
    FormsModule, // if used
    HttpClientModule,
    JsonpModule // if used
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Angular2 >= RC.5

将HttpModule导入到你使用它的模块中(例如AppModule):

import { HttpModule } from '@angular/http';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule, // if used
    HttpModule,
    JsonpModule // if used
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

导入HttpModule非常类似于在以前的版本中添加HTTP_PROVIDERS。

因为钢筋混凝土。5 .你得做点什么

@NgModule({
    imports: [ BrowserModule],
    providers: [ HTTP_PROVIDERS ],  
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})
export default class AppModule { }

platformBrowserDynamic().bootstrapModule(AppModule);

从RC5开始,你需要像这样导入HttpModule:

import { HttpModule } from '@angular/http';

然后将HttpModule包括在上面提到的Günter的imports数组中。

**

简单的方法:在app.module.ts中导入HttpModule和HttpClientModule

**

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



@NgModule({
 declarations: [
   AppComponent, videoComponent, tagDirective, 
 ],
 imports: [
  BrowserModule, routing, HttpClientModule, HttpModule

],
providers: [ApiServices],
bootstrap: [AppComponent]
})
export class AppModule { }

导入HttpModule

import { HttpModule } from '@angular/http';

@NgModule({
    imports: [ BrowserModule, HttpModule ],
    providers: [],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})
export default class AppModule { }

platformBrowserDynamic().bootstrapModule(AppModule);

理想情况下,您可以将这些代码拆分到两个单独的文件中。 欲了解更多信息,请阅读:

https://v2.angular.io/docs/ts/latest/cookbook/rc4-to-rc5.html https://v2.angular.io/docs/ts/latest/guide/ngmodule.html