我正在从头开始学习 angular 5。我对 Http 响应有一些问题。我无法使用 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'."
这是因为您的响应类型是Object,因此您不能将其分配给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);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句