我正在從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"
},
其顯示'屬性'訂閱'不存在類型'承諾<{}>'.'打字稿錯誤。 –
如果你使用'toPromise()',那麼你需要使用'then'就像你的問題中的代碼。如果你不使用'toPromise()',那麼你可以像我的答案中的第二個例子那樣使用'subscribe()'。看來你不知怎麼混合了這兩種方法。 –
我沒有使用'toPromise()',但仍然顯示'Property'訂閱'不存在類型'Promise <{}>'Typescript錯誤。 –