我刚开始学Angular 2和Typescript,请见谅,但我不明白在成功调用REST API后如何消费数据。
我使用了plunker来展示我的例子,这样更容易解释我想做什么。
查看示例时请忽略未使用的导入。
调用getWeather
函数是有效的。
getWeather(query) {
const endpoint = 'http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98';
return this.http
.get(endpoint)//, {search: searchParams})
.map(res => res.json().main)
.subscribe(res => console.log('weather json response = ' + JSON.stringify(res))
);
}
但是数据该如何存储呢?我的意思是,我是否需要创建一个类似于json响应的对象来显示数据,如果需要,要如何使用? 编辑:这里有一个带有我的代码的可运行示例。