2017-07-23 41 views
-2

我的組件服務:如何寫角2

getDetailsByPhone(phone: any) { 
     this.phoneSearchDetails = this.appService.manageHttp('get', 'search?number='+phone, ''); 
} 

我的服務:

manageHttp(method: any, url: any, body: any) { 
    this.headers = this.config.setHeaders(); 
    var urlto = this.config.serverUrl +''+url; 

    return this.http.get(urlto,{ headers: this.headers }).map((response: Response) => { 
      return response.json(); 
     }); 
} 

有了這個代碼,我不能看到network.I會響應我不知道什麼是錯的,我沒有看到控制檯中的任何錯誤。任何人都可以請建議help.Thanks。

+2

[Angular 2 http get not get]可能重複(https://stackoverflow.com/questions/41381200/angular-2-http-get-not-getting) – 0mpurdy

回答

1

您必須訂閱觀察到,否則將不火,因爲它是懶惰:

this.appService.manageHttp('get', 'search?number='+phone, '').subscribe((res) => { 

    this.phoneSearchDetails = res; 

    console.log(res); 
}, (err) => { 
    // handle error 
}); 

此外,你可以看到我指定subscribe回調內部的phoneSearchDetails的價值,因爲你不能返回任何東西從subscribe