在 Angular 中,如何避免“类型 'Object' 上不存在属性 'json'”错误?

戴夫

我正在使用 Angular 7 并尝试从 Rails 5 应用程序读取 JSON 数据。我的 src/app/app.component.ts 文件中有这个

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  books;

  constructor(private http: HttpClient) {
    http.get('http://localhost:3000/books.json')
      .subscribe(res => this.books = res.json());
  }
}

但是,当我使用“ng serve”启动我的 Angular 应用程序时,我收到了这个错误

ERROR in src/app/app.component.ts(15,42): error TS2339: Property 'json' does not exist on type 'Object'.

我已经确认,当我访问 hte 端点http://localhost:3000/books.json 时,我得到了输出

[{"id":1,"name":"Copying and Pasting from Stack Overflow","created_at":"2019-08-14T19:55:57.961Z","updated_at":"2019-08-14T19:55:57.961Z"},{"id":2,"name":"Trying Stuff Until it Works","created_at":"2019-08-14T19:55:57.966Z","updated_at":"2019-08-14T19:55:57.966Z"}]

所以我很困惑还有什么可能是错的。

亚历山大·斯塔罗塞尔斯基

默认情况下,HttpClient会在幕后为您提取 JSON,而无需使用json(). HttpModule在 Angular 2.x 中要求您使用 提取json(),但HttpClient在 4.x+ 中为您执行此操作。您可以简单地直接访问解析后的 JSON 数据subscribe()

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  books;

  constructor(private http: HttpClient) {
    http.get('http://localhost:3000/books.json')
      .subscribe(res => this.books = res);
  }
}

真的建议您利用 TypeScript 并对响应进行类型检查

interface Book {
  someProperty: string;
  anotherProperty: number;
}

// ...

books: Book[];

// ...

http.get<Book[]>('http://localhost:3000/books.json')
  .subscribe(res => this.books = res);

希望这有帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何避免打字错误:类型“元素”上不存在属性“ innerHTML”

类型“ {}”上不存在属性“ json”

Angular2:错误-类型“ AbstractControl”上不存在属性“ updateValue”

Angular2属性在类型“ any []”(JSON)上不存在

错误TS2339:带有Angular的类型“ HeroService”上不存在属性“ handleError”

调用服务angular2错误'属性'then'在类型'Subscription'上不存在

Typescript和Angular2-highcharts:类型“ Object”上不存在属性“ series”

生产版本上的Angular4编译错误:“类型抽象控件上不存在属性控件”

在ng build --prod am gettng错误上,类型'Object'不存在属性'email'

Angular 5构建错误“类型上不存在属性”

错误TS2339:类型“对象”上不存在属性“ json”

Angular 5-模板错误:“类型'AbstractControl'上不存在属性'length'”

Angular2:类型对象上不存在json

收到错误:“属性'用户'在类型'ArrayBuffer'上不存在。在Angular中

Angular 7-'对象类型不存在属性json'

在Angular中设置“配置文件详细信息”页面时,TypeScript“类型上不存在属性”错误

错误:类型“ Observable <Object>”上不存在属性“ map”

Angular2 映射 -> 订阅数据类型错误(类型“ErrorObservable”上不存在属性“length”。)

错误 TS2339:类型“Angular2”上不存在属性“”

函数抛出错误 [ts] Angular4 中的“Object”类型不存在属性“json”

离子 3 HttpClient 连接 api 错误,“对象”类型上不存在属性“json”

错误:'错误 TS2339:'Response' 类型上不存在属性 'data'。- Angular

错误 TS2339:“对象”类型上不存在属性“JSON”。".map(resp => resp.JSON())" 角度 4

angular -“changeThisBreaksApplicationSecurity”给出了 ts 错误“属性 'changeThisBreaksApplicationSecurity' 在类型 'SafeUrl' 上不存在。”

Angular,类型上不存在属性

类型“() => void”上不存在属性“push”。如何解决 Typescript Angular 10 中的这个错误?

Angular - 错误 TS2339:“对象”类型上不存在属性“结果”

Angular 12. 错误 TS2339:“EventTarget”类型上不存在属性“value”

ReactJS 中的 Typescript 错误 - 类型上不存在属性