angular - 如何在角度 11 中获得适当的响应
问题描述
我是 Angular 新手,正在观看一系列教程。我正在为我的数据库使用 Firebase。下图是我从数据库中获取数据列表的代码。但不幸的是,我收到了如下图所示的错误。
我还尝试将我的回复类型更改为any
.subscribe((response: any) => {
但是在获取我的数据时它会抛出一个错误说ERROR TypeError: response.json is not a function
顺便说一句,我正在跑步"@angular/cli": "~11.0.1"
解决方案
下面的代码可以解决问题。
getRecipe() {
this.http.get<Recipe[]>(/*YOUR API CALL*/)
.subscribe((recipes: Recipe[]) => this.recipeService.setRecipes(recipes));
}
您已经可以通过在< >
. JSON 将自动转换为您的类。
正如@Waseem Rakab 指出的那样,没有必要拥有response.json
等。
推荐阅读
- python - python程序无法读取以下xml文件
- bots - 如何创建机器人来玩游戏?
- felogin - felogin 同一个域上的不同/多个登录表单
- c++ - #defined 枚举 Doxygen
- python-3.x - Airflow 2 插件如何从插件文件夹中导入其他文件?
- pandas - 将带有嵌套数组的 Spark 列转换为 numpy 数组?
- python - 只有我的最后一个数据框保存在我的所有工作表 xlsx(python)中
- python - 使用 Scapy 在 Wireshark 中给出的访问协议
- powershell - 使用 powershell 创建 zip
- java - 嵌套for循环和数组索引超出范围异常的问题