我在Angular应用程序中获得EXCEPTION: No provider for 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

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

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

platformBrowserDynamic().bootstrapModule(AppModule);

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

Angular相关问答推荐

如何从Angular TS文件传递$Events对象?

Angular中绑定嵌入标签src属性的问题

Angular 17上的material 工具提示的自定义样式

Angular 16未知参数:独立

添加@ nx/webpack插件 destruct 了Nativescript构建

Angular 客户端应用程序无法从停靠容器解析后端服务的地址

尽管有模拟间谍实现,规范文件仍调用真正的服务

Rxjs重置执行后的可观察延迟并重新调度Angular

Angular 环境本地无文件替换

Angular 16:CSRF配置:我仍然可以使用HttpXsrfInterceptor和HttpXsrfCookieExtractor类吗?Inteli-J说他们不存在

在RxJS和ANGING中处理多个API调用

筛选器不会从文本输入触发更改事件

使用Dragula时,ReactiveForm元素在拖动副本中显示不同的