0
我試圖在我的angular2應用程序中注入http模塊,但是出現錯誤。不能在angular2中注入http
預期的值 'HTTP' 由模塊進口 '的AppModule'
這就是我正在努力。
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { Http } from '@angular/http';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, FormsModule, Http],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
這是我嘗試使用它
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `<h1>Hello {{name}}</h1>
Maintenance Name: <input type="text" [(ngModel)]="htmlToPublish.title"/>
<textarea [(ngModel)]="htmlToPublish.html" rows="10" cols="50">Write here the HTML code that you want to publish
</textarea>
<button (click)="sendHtml()">Publish</button>
<p>
{{htmlToPublish}}
</p>
`,
})
export class AppComponent {
constructor(public http: Http){
}
name = 'Angular';
htmlToPublish: HtmlPage
public sendHtml() {
// Here I want to use http that I injected in constructor
}
}
非常感謝。對於我來說,你在NgModule中導入了一件事,而在組件中導入了另一件事,這有點令人困惑。我不知道這已經改變 – acostela
'Http'只是最常用的類,但它包含一堆類。以前這是'HTTP_PROVIDERS',而不是'HttpModule'。 –