【发布时间】:2019-08-14 21:18:41
【问题描述】:
我正在使用 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"}]
所以我很困惑还有什么可能是错的。
【问题讨论】: