2017-04-05 54 views
1

我正在從google Api中獲取數據,在details.ts文件的函數中,我爲此創建了服務,如下所示。其displaing打字稿錯誤Property 'then' does not exist on type 'void'property'then'在類型'void'中不存在ionic2

this.typeApi.getTypeDetails(baseUrl).then(data => { 
}); 

type-api.service.ts文件,我從谷歌阿比作爲波紋管取數據,

import { Injectable } from '@angular/core'; 
import { Http /*, Response*/ } from '@angular/http'; 
import 'rxjs/Rx'; 
import { Observable } from 'rxjs/Observable'; 



@Injectable() 
export class TypeApi { 

constructor(public http: Http) {} 

getTypeDetails(PlaceUrl){ 
     this.http 
     .get(PlaceUrl) 
     .map(res => res.json()) 
     .subscribe(
      (data) => data, 
      (err) => err); // Reach here if fails 
} 

} 

中的package.json文件,

"dependencies": { 
"@angular/common": "2.4.8", 
"@angular/compiler": "2.4.8", 
"@angular/compiler-cli": "2.4.8", 
"@angular/core": "2.4.8", 
"@angular/forms": "2.4.8", 
"@angular/http": "2.4.8", 
"@angular/platform-browser": "2.4.8", 
"@angular/platform-browser-dynamic": "2.4.8", 
"@angular/platform-server": "2.4.8", 
"@ionic-native/core": "3.1.0", 
"@ionic-native/geolocation": "^3.4.4", 
"@ionic-native/launch-navigator": "^3.4.4", 
"@ionic-native/splash-screen": "3.1.0", 
"@ionic-native/status-bar": "3.1.0", 
"@ionic/storage": "2.0.0", 
"font-awesome": "^4.7.0", 
"ionic-angular": "2.3.0", 
"ionic2-rating": "^1.2.0", 
"ionicons": "3.0.0", 
"rxjs": "5.0.1", 
"sw-toolbox": "3.4.0", 
"zone.js": "0.7.2" 
    }, 
"devDependencies": { 
"@ionic/app-scripts": "1.1.4", 
"typescript": "2.0.9" 
}, 

回答

1

同意蒙山岡特,
另外我建議你設置方法(函數)返回類型無處不在,以便查看調用者將收到的結果類型。 對於上面的護理:

假設你TypeApi將在多個來電

public getTypeDetails(apiUrl: string) : Observable<any>{ 
     return this.http.get(apiUrl) 
      .map(res => res.json()) 
      .catch((error) => this.handleError(error));; 
} 

//爲例handleError功能被重用

private handleError (error: any) { 
    // Could dig deeper into the error to get a better message or use a remote logging infrastructure 
    let errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error'; 
    console.error(errMsg); // log to console instead 
    return Observable.throw(errMsg); 
    } 

然後,當你調用這個方法,您必須訂閱該方法將返回的結果:

this.typeApi.getTypeDetails(theUrl).subscribe(result => { 
    //here you have the analog success function from an ajax call 
    //use the received data 
    }, 
    error => { 
     //here you have the analog error function from an ajax call 
     //treat the error 
    } 
); 
2

這個工作你需要退回PromisegetTypeDetails

getTypeDetails(PlaceUrl){ 
    return this.http 
     .get(PlaceUrl) 
     .map(res => res.json()) 
     .toPromise(); 
} 

只是返回Observable通常是更好的辦法

getTypeDetails(PlaceUrl){ 
    return this.http 
     .get(PlaceUrl) 
     .map(res => res.json()); 
} 

this.typeApi.getTypeDetails(baseUrl).subscribe(data => { 
}); 
+0

其顯示'屬性'訂閱'不存在類型'承諾<{}>'.'打字稿錯誤。 –

+0

如果你使用'toPromise()',那麼你需要使用'then'就像你的問題中的代碼。如果你不使用'toPromise()',那麼你可以像我的答案中的第二個例子那樣使用'subscribe()'。看來你不知怎麼混合了這兩種方法。 –

+0

我沒有使用'toPromise()',但仍然顯示'Property'訂閱'不存在類型'Promise <{}>'Typescript錯誤。 –

相關問題