簡體   English   中英

將Promise信息轉換為可觀察的

[英]Converting Promise information to observable

我遵循了《英雄之旅》指南,但已確定可觀察的目標要比承諾的要好,但是我很難實現它們。

這是我的食譜服務:

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));
  }

我不確定如何像我在本教程中對promise所做的那樣,從可觀察數組中獲取特定的可觀察項。

在這種情況下,您應該使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM