【发布时间】:2021-01-27 11:28:53
【问题描述】:
我正在尝试在 Angular 项目中创建一个服务,该服务从 API 服务器检索 CSV 数据并将 JSON 传递给组件。
控制台显示来自服务的漂亮 JSON 数据,但组件显示更早的 组件数据:UNKNOWN。
我使用 csvtojson 模块将 CSV 转换为 JSON
组件
getData(): any {
this.storeService.getData()
.subscribe(data => {
console.log('Component data: ', data);
}, error => {
console.log(error);
});
}
服务“storeService”
getData(): Observable<any> {
return this.httpClient
.get('http://llocalhost:3000/user/11', { responseType: 'text' as 'text' })
.pipe(
map((data) => {
csv({
noheader: true,
trim: true,
delimiter: ';'
})
.fromString(data)
.then(jsonData => {
// This is OK - I can see a JSON data
console.log('Service jsonData', jsonData);
// How to send this jsonData to component?
return jsonData;
})
})
)
}
感谢您的帮助。
【问题讨论】:
-
在csv()中添加
return语句。所以把这个return csv({}),你返回的jsonData返回到地图
标签: json angular csv observable httpclient