在angular5中遇到Http Response问题

时间:2018-04-06 11:52:19

标签: javascript angular5

我从头开始学习角度5。我对Http Response有一些问题。我无法使用get()获取数据。

      getRecipes() {this.http.get('https://indhu-project-45b73.firebaseio.com/recipes.json')
          .subscribe(response => {
           const recipes: Recipe[] = response;
          this.recipeService.setRecipes(recipes);
          });

我提供了导入为,

  import {Injectable} from '@angular/core';
  import {HttpClient} from '@angular/common/http';
  import {RecipeService} from '../recipes/recipe.service';
  import 'rxjs/add/operator/map';
  import {Recipe} from '../recipes/recipe.model';

  setRecipes(recipes: Recipe[]) {
   this.recipes = recipes;
   this.recipesChanged.next(this.recipes.slice());
   }

我知道我的结局肯定会有一些基本错误。有人可以帮助我。我收到以下错误

     "Type 'Object' is not assignable to type 'Recipe[]'.
      The 'Object' type is assignable to very few other types. Did you 
       mean to use the 'any' type instead?
      src/app/shared/data-storage.service.ts(17,15): error TS2322: Type 
      'Object' is not assignable to type 'Recipe[]'.
       The 'Object' type is assignable to very few other types. Did you 
        mean to use the 'any' type instead?
        Property 'includes' is missing in type 'Object'."

1 个答案:

答案 0 :(得分:0)

这是因为您的回复类型是对象,因此您无法将其分配给 Recipe []类型的食谱。 您可以使用' any' 类型代替您的响应。它会工作。

getRecipes() {this.http.get('https://indhu-project-45b73.firebaseio.com/recipes.json')
      .subscribe((response: any) => {
       const recipes: Recipe[] = response;
      this.recipeService.setRecipes(recipes);
      });