[英]Converting Promise information to observable
I followed the Tour of Hero guide but have decided Observables are better than promises but I am having trouble implementing them. 我遵循了《英雄之旅》指南,但已确定可观察的目标要比承诺的要好,但是我很难实现它们。
Here is my recipe service: 这是我的食谱服务:
import { of } from 'rxjs/Observable/of';
...
getRecipes(): Observable<Recipe[]> {
return of(RECIPES);
}
getRecipe(id: number): Observable<Recipe> {
return this.getRecipes()
.subscribe(recipes => recipes.find(recipe => recipe.ID === id));
}
I am not sure how to get a specific observable item from an obvserable array like I did with promises on the tutorial. 我不确定如何像我在本教程中对promise所做的那样,从可观察数组中获取特定的可观察项。
You should use map for this case, your implementation can be following 在这种情况下,您应该使用map,实现如下
getRecipe(id: number): Observable<Recipe> {
return this.getRecipes()
.map(recipies => recipies.find(x => x.id === id));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.