2016-10-31 51 views
0

我的项目的服务器端使用nodejs。 我使用https.createServer(选项,功能(需求,水库){....}),它的工作原理。如何使角度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(); 
      }); 
     } 
}