简体   繁体   English

angular5中的HTTP响应出现问题

[英]Having issue with Http Response in angular5

I am learning angular 5 from scratch. 我正在从头开始学习角度5。 I have some issue with Http Response. 我对Http Response有一些问题。 I could not fetch data with get(). 我无法使用get()获取数据。

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

i have provided the imports as, 我提供了进口,

  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';

also

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

I know there must be some basic mistakes from my end. 我知道到最后一定会有一些基本的错误。 Can Someone help me in this. 有人可以帮我吗? I am getting this following error 我收到以下错误

     "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'."

It's because the type of your response is Object so you can't assign it to recipes of type Recipe[]. 这是因为响应的类型是Object,所以您不能将其分配给Recipe []类型的配方。 You can use type 'any' for your response instead. 您可以改为使用“ any”作为响应。 It will work then. 它将起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM