簡體   English   中英

無法在angular2中注入http

[英]cant inject http in angular2

我正在嘗試在我的angular2應用程序中注入http模塊,但出現錯誤。

模塊“ AppModule”導入的異常值“ Http”

這就是我正在嘗試的。

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

   }

}

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';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM