我正在尝试使用角2从Statbureau通胀api- https://www.statbureau.org/en/inflation-api中检索数据。我刚开始测试我的api并确保它在控制台中输出一个成功的消息。看来,我使用的api是JSONP,因为我在网站上使用http://www.statbureau.org/calculate-inflation-price-jsonp?jsoncallback=的JSON地址,他们提供了一个JS小提琴,使用jquery对api进行了成功的调用。当我试图在角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参数只是为了快速看到结果。
https://stackoverflow.com/questions/38381296
复制相似问题