2016-07-14 92 views
0

要求我試圖找回從Statbureau通脹數據API - https://www.statbureau.org/en/inflation-api採用了棱角分明2.我開始斷不僅是測試我的API,並確保它在控制檯輸出一個成功的消息。看來我使用的api是JSONP,因爲我使用的JSON地址是http://www.statbureau.org/calculate-inflation-price-jsonp?jsoncallback=在網站上,他們提供了一個JS小提琴,它使用jquery對api進行了成功的調用。當我試圖在角2我收到以下錯誤相同的呼叫:HTTP GET在角2

TypeScript error: C:/Users/Rennie/projects/inflator/app/pages/home/home.ts(22,13): Error TS 
2322: Type 'Observable<any>' is not assignable to type 'HomePage'. 
    Property 'jsonp' is missing in type 'Observable<any>'. 
TypeScript error: C:/Users/Rennie/projects/inflator/app/pages/home/home.ts(22,13): Error TS 
2409: Return type of constructor signature must be assignable to the instance type of the c 

這裏是我的代碼

import {Component} from '@angular/core'; 
import {NavController} from 'ionic-angular'; 
import {Jsonp, URLSearchParams } from '@angular/http'; 
import {Observable} from 'rxjs/Rx'; 
import 'rxjs/add/operator/map'; 
@Component({ 
    templateUrl: 'build/pages/home/home.html' 
}) 
export class HomePage { 


    constructor(private jsonp: Jsonp) { 

    this.jsonp=jsonp; 
     let cpiUrl = "https://www.statbureau.org/calculate-inflation-price-jsonp?jsoncallback" 
     let params = new URLSearchParams(); 
     params.set('country', 'united-states'); 
     params.set('amount', '102'); 
     params.set('start', '1968/1/1'); 
     params.set('finish', '2016/1/1'); 
     // TODO: Add error handling 
     return this.jsonp 
       .get(cpiUrl, { search: params }).map(function(response){ return response.json(); }) 

    } 


} 

我的代碼從的jsfiddle電話不同,因爲我硬編碼api參數只是爲了快速看到結果。

回答

1

1)構造函數通常(總是?)沒有返回語句。卸下回statment應該處理type 'Observable<any>' is not assignable to type 'HomePage'.

2)似乎沒有要在Angular 2檢查docsJsonp類中的方法get

3)一個可觀察的序列/管道將不會執行,直到訂閱(他們是懶惰的),所以即使該代碼編譯的請求不會被執行。

4)我想你應該看看this類似的問題。

祝你好運。

+0

我轉發了這個,因爲一個新的錯誤,看一看 - http://stackoverflow.com/questions/38401935/jsonp-request-error-angular-2 – RSB