首页 > 解决方案 > 如何在角度 11 中获得适当的响应

问题描述

我是 Angular 新手,正在观看一系列教程。我正在为我的数据库使用 Firebase。下图是我从数据库中获取数据列表的代码。但不幸的是,我收到了如下图所示的错误。

我还尝试将我的回复类型更改为any

 .subscribe((response: any) => {

但是在获取我的数据时它会抛出一个错误说ERROR TypeError: response.json is not a function

在此处输入图像描述

在此处输入图像描述

顺便说一句,我正在跑步"@angular/cli": "~11.0.1"

标签: angular

解决方案


下面的代码可以解决问题。

getRecipe() {
   this.http.get<Recipe[]>(/*YOUR API CALL*/)
      .subscribe((recipes: Recipe[]) => this.recipeService.setRecipes(recipes));
}

您已经可以通过在< >. JSON 将自动转换为您的类。

正如@Waseem Rakab 指出的那样,没有必要拥有response.json等。


推荐阅读