我需要从多个来源获取api,但不知道如何编写它简单。而不是做:
getFoodData(){
return this.http.get('https://something.com/category=food')
.map(res => res.json())
.map(data => <Data>{value: data.value});
}
getFilmData(){
return this.http.get('https://something.com/category=film')
.map(res => res.json())
.map(data => <Data>{value: data.value});
}
而不是在组件中重复自己10次:
getFoodData() {
this.dataService.getFoodData()
.subscribe(data => {
this.datas.push(data)
})
getFilmData() {
this.dataService.getFoodFilmData()
.subscribe(data => {
this.datas.push(data)
})
并通过单击按钮在html中呈现数据
<button (click)="getFilmData()">more Film</button>
<button (click)="getFoodData()">more Food</button>
执行此操作的最简单和最佳做法是什么