Angular EXCEPTION:没有Http的提供程序 [英] Angular EXCEPTION: No provider for Http

查看:54
本文介绍了Angular EXCEPTION:没有Http的提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular应用中获取了EXCEPTION: No provider for Http!.我在做什么错了?

I am getting the EXCEPTION: No provider for Http! in my Angular app. What am I doing wrong?

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

Import the HttpModule

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

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

platformBrowserDynamic().bootstrapModule(AppModule);

理想情况下,您将此代码分为两个单独的文件. 有关更多信息,请阅读:

Ideally, you split up this code in two separate files. For further information read:

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

这篇关于Angular EXCEPTION:没有Http的提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆