2016-11-30 1 views
0

私のangular2アプリケーションにhttpモジュールを挿入しようとしていますが、エラーが表示されています。angt2に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 

    } 

} 

答えて

3

それを使用しようとする方法であるHttpを取得するために輸入にHttpModuleを追加

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'; 
が必要
+0

ありがとうございました。 NgModuleで1つのものをインポートし、コンポーネントで別のものをインポートするのはちょっと混乱します。私はこれが変更されたことを知らなかった – acostela

+1

'Http'は最も一般的に使われるクラスですが、それはクラスの束で構成されています。以前はこれが 'HTTP_PROVIDERS'でした。' HttpModule'ではありません。 –

関連する問題