2016-10-31 13 views
0

私のプロジェクトのサーバー側でnodejsを使用しています。 https.createServer(option、function(req、res){....})を使用しています。角度2 "https"リクエストの作成方法

私のプロジェクトのクライアント側は角度2を使用しています。角度2にはhttpがありますが、角度2にはサーバー側に接続するhttps呼び出しを使用したいと考えています。私は解決策が使用していることを知っています this.http .get(https://url)。しかし、私は自分のコードでそれを使う方法を知らない。

export class HttpDataService<T> extends DataService<T> { 
    protected _url: UrlType; 


    constructor(protected http: Http) { 
     super(); 
     this.fetch(); 
    } 

    this.http.get(url) 
      .map((res) => { 
       return res.json(); 
      }) 
      .catch((error: any) => { 
       console.log(error); 
       this._fetching = false; 
       return Observable.throw(error); 
      }) 
      .subscribe(data => { 
       this.setData(data); 
       this._fetching = false; 

       this._count++; 
       if ((this._count < 
         this._url.count) || 
        (this._url.count ==         
         **strong text**UrlTypeCount.Infinite)) 
        this.fetch(); 
      }); 


} 
+1

'this.http.get(...) 'のように見えますが、コンストラクタの外にあります。 TypeScriptでは、メソッドの外部にある任意の式や文を使用することはできません。あなたのコードの問題は何ですか?期待どおりに動かないのは何ですか? –

+0

私のコードはhttpを使って動作します。しかし、私はhttpsを使いたいのですが、私はangular2でそれを使う方法がわかりません!! @GünterZöchbauer – justinlin

+0

何が問題なのですか?エラーメッセージが表示されますか? –

答えて

0

コードを関数に入れる必要があります。あなたのクラスのボディは今何かを呼んでいますが、そうではありません。たとえば、次のようになります。

export class HttpDataService<T> extends DataService<T> { 
    protected _url: UrlType; 


    constructor(protected http: Http) { 
     super(); 
     this.fetch(); 
    } 

    getStuff() { 
     this.http.get(url) 
      .map((res) => { 
       return res.json(); 
      }) 
      .catch((error: any) => { 
       console.log(error); 
       this._fetching = false; 
       return Observable.throw(error); 
      }) 
      .subscribe(data => { 
       this.setData(data); 
       this._fetching = false; 

       this._count++; 
       if ((this._count < 
         this._url.count) || 
        (this._url.count ==         
         **strong text**UrlTypeCount.Infinite)) 
        this.fetch(); 
      }); 
     } 
} 
関連する問題