首页 > 解决方案 > 另一个角度内的http请求

问题描述

我正在从数据库中获取食谱(在效果中 - 请参见下面的代码),其中每个食谱都有成分。下面是对应的Recipe模型代码:

export class Recipe {
    id_recipe: number;
    name: string;
    image: string;
    difficulty: number;
    category_id: number;
    country_id: number;
    ingredients: Ingredient[];
}

这是我的角度代码

@Effect()
    fetchRecipes = this.actions$.pipe(
        ofType(RecipesActions.FETCH_RECIPES),
        switchMap(() => {
            return this.http.get<Recipe[]>('http://localhost/recipes/get.php',
            {
                params: new HttpParams().set('q', 'SELECT * FROM recipe')
            })
        }),
        map(recipes => {
            return recipes.map(recipe => {
                return {
                    ...recipe,
                    ingredients: this.http.get<Ingredient[]>('http://localhost/recipes/get.php',
                    {
                        params: new HttpParams().set('q', 'SELECT name, amount, units FROM ingredient INNER JOIN recipe_has_ingredient ON ingredient.id_ingredient = recipe_has_ingredient.id_ingredient WHERE recipe_has_ingredient.id_recipe = ' + recipe.id_recipe)
                    })
                }
            })
        }),
        map(recipes => {
            return new RecipesActions.SetRecipes(recipes);
        })
    );

我想根据配方 ID 获取配料。目前我将成分作为可观察的,但我想要一个数组。有人可以帮我吗?

标签: angular

解决方案


推荐阅读