我试图在我的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
}
}
答案 0 :(得分:3)
将HttpModule
添加到导入以获取Http
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http'; // <<< changed
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, FormsModule, HttpModule], // <<< changed
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
但是你需要注射Http
的地方
import { Http } from '@angular/http';