angular - Angular 7 在服务中获取 Json 数组
问题描述
我有一个返回这个 JSON (GET@localhost:8000/:number) 的 API:
[
{
"trip": 5,
"time_logged": "17/12/2018 07:23:11",
"day_logged": 17,
"month_logged": 12,
"year_logged": 2018,
"hour": 10,
"minute": 0,
"fixquality": 6,
"speed": 50,
"angle": 12,
"lon": -121.180632,
"lat": 37.503852,
"altitude": 100000,
"temp": 13
},
{
"trip": 5,
"time_logged": "17/12/2018 07:24:59",
"day_logged": 17,
"month_logged": 12,
"year_logged": 2018,
"hour": 10,
"minute": 0,
"fixquality": 6,
"speed": 50,
"angle": 12,
"lon": -122.180632,
"lat": 37.503852,
"altitude": 100000,
"temp": 13
}
]
在我的 Angular 7 项目中,我有一个服务应该接受这个 JSON 并将其返回为Observable<HABUpdate[]>
:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { Observable } from 'rxjs';
export interface HABUpdate {
trip: number;
time_logged: string;
day_logged: number;
month_logged: number;
year_logged: number;
hour: number;
minute: number;
fixquality: number;
speed: number;
angle: number;
lon: number;
lat: number;
altitude: number;
temp: number;
}
@Injectable({
providedIn: 'root'
})
export class HabdataService {
url: string = 'http://localhost:8000/';
constructor(private http: HttpClient) { }
getUpdates(trip: number): Observable<HABUpdate[]> {
return this.http.get<HABUpdate[]>(this.url + String(trip));
}
}
但是,我在运行时收到此控制台错误:
ERROR Error: "[object Object]"
resolvePromise http://localhost:4200/polyfills.js:3159:31
resolvePromise http://localhost:4200/polyfills.js:3116:17
scheduleResolveOrReject http://localhost:4200/polyfills.js:3218:17
invokeTask http://localhost:4200/polyfills.js:2766:17
onInvokeTask http://localhost:4200/vendor.js:51518:24
invokeTask http://localhost:4200/polyfills.js:2765:17
runTask http://localhost:4200/polyfills.js:2533:28
drainMicroTaskQueue http://localhost:4200/polyfills.js:2940:25
invokeTask http://localhost:4200/polyfills.js:2845:21
invokeTask http://localhost:4200/polyfills.js:3885:9
globalZoneAwareCallback http://localhost:4200/polyfills.js:3911:17
core.js:14597
我是 Angular 的新手,所以起初我认为我不能正确解析 JSON 以便将其转换为HABUpdate[]
. 然而,通过互联网,我发现人们做类似事情的例子,他们的代码似乎有效。作为参考,这是试图引用此服务的组件:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HabdataService, HABUpdate } from '../habdata.service';
@Component({
selector: 'app-launches',
templateUrl: './launches.component.html',
styleUrls: ['./launches.component.scss']
})
export class LaunchesComponent implements OnInit {
trip$: number;
updates$: HABUpdate[];
constructor(private route: ActivatedRoute, private data: HabdataService) {
this.route.params.subscribe(
params => this.trip$ = params.trip
);
}
ngOnInit() {
this.data.getUpdates(this.trip$).subscribe(
data => this.updates$ = data
);
}
}
我确信我一定缺少一些基本的东西,但是我有一段时间没能找到解决方案。任何帮助表示赞赏。
解决方案
推荐阅读
- oauth-2.0 - 为什么 Postman 需要用户登录才能使用 Google oauth?
- c# - FluentNHibernate 映射.Net Core
- javascript - 如何在 dataTable.js 中转置行和列
- php - 如何在文本框中选择 1 到 100 的数字范围
- java - 如何使用枚举作为条件拆分方法功能?
- javascript - 在 Flipclock.js 上隐藏秒计数器
- django - 将 django 用户作为重定向请求标头传递
- php - 使用 php twitter 从 JSON URL 打印值
- c++ - mingw-w64 g++ 在编译 wxWidgets 期间意外终止
- c#-4.0 - 无法在 .NET 上接收 CometD 消息