2016-11-03 14 views
0

私は角度2のプロジェクトHttpでいくつかの問題があります。パラメータとしてコンストラクタで設定し、コンストラクタ内では定義されていないとして出力します。Httpが角2で動作していません

ここ
import 'rxjs/add/operator/toPromise'; 
import {Http, HttpModule, RequestOptions, XHRBackend} from "@angular/http"; 
import { Injectable }  from '@angular/core'; 

@Injectable() 
export class GetDataService{ 
private dataUrl = '/data'; // URL to web API 
private req; 
//mode = 'observable'; 
public headers; 
public options; 
constructor(http:Http) { 
    //this.http = new Http(XHRBackend, RequestOptions); 
    console.log(http); 
} 
} 

は私のエラーサービスが

を使用

EXCEPTION: Uncaught (in promise): Error: Error in http://localhost:3000/app/invoice.component.html:160:0 caused by: Cannot read property 'get' of undefined


コンポーネントでありますメインモジュールの提供も

私は、コンストラクタのパラメータでGetDataServiceを注入しようとする中

import {Component} from '@angular/core'; 
import {GetDataService} from "./get-data.service"; 

@Component({ 
selector: 'http', 
template: `<h4>response</h4>` 
}) 

export class GetDataComponent { 
response:string; 
getDataService:GetDataService; 
constructor(){ 
    this.getDataService = new GetDataService(); 
    console.log(this.getDataService.getData()); 
} 
} 

と追加サービス

constructor(getDataService:GetDataService){ 
    this.getDataService = getDataService; 
    console.log(this.getDataService.getData()); 
} 

私が取得 -

Error: (SystemJS) Can't resolve all parameters for GetDataComponent


私が持っていますまた、コンストラクタ内部でHttpのオブジェクトを作成しようとしましたが、しかし、やはり私はエラーが発生しています。

constructor() { 
    this.http = new Http; 
    console.log(this.http); 
} 
this is the result of above code: 

Http {_backend: undefined, _defaultOptions: undefined} 

誰でも助けてください。

+0

https://angular.io/docs/ts/latest/tutorial/toh-pt6.htmlを参照してください「動作しない」は非常に明確ではありません。期待される行動は何ですか?実際の行動とは何ですか?どこでどのように 'http'を使ってHTTPリクエストを作成しますか? –

+0

私はこのサービスでgetDataメソッドでリクエストを取得しようとしましたが、httpが定義されていないのでhttp.methodは動作しません –

答えて

0

Httpは、new Http()で作成されているはずです。

HttpModule@NgModule({ imports: [BrowserModule, HttpModule], ...})に追加し、最初の例のようにコンストラクタを使用してHttpを注入します。詳細については

+1

httpをパラメータに挿入しようとするとエラーが発生しますhttp undefined - "未定義のget ' " –

+0

私の答えに示されているように' HttpModule'を 'imports'に追加しましたか? –

+0

もちろん私は –

関連する問題