通过服务从 json 文件获取数据在 Angular4 中给出 "unexpected end of input"
Fetching data from json file via service is giving "unexpected end of input" in Angular4
我正在尝试从资产文件夹中的 json 文件中获取数据。但反而出现错误。
heroes.service.ts
getPeopleData(): Observable<people[]>{
return this.http.get<people[]>('assets/data/someData.json');
}
people.interface.ts
export interface people{
id: number,
name: string,
age: number
}
someData.json:
[
{"id":1, "name": "Jack", "age": 21},
{"id":2, "name": "Rina", "age": 29},
{"id":3, "name": "Jonathan", "age": 42}
]
about.component.ts
peopleData:any;
ngOnInit() {
this.getPeople();
}
getPeople(): void {
this.heroesService.getPeopleData()
.subscribe(data => this.peopleData = data)
}
错误:
谁能帮帮我,问题出在哪里?相反,控制台中没有错误。
这对我有用:
服务:
public testJson(): Observable<people[]>{
return this.http.get("https://api.myjson.com/bins/1f5zag").map(res => res.json());
}
(你可以用那个url实际试试)
component.onInit
this.yourService.testJson()
.subscribe( evt => {
const ppl:people[] = evt;
console.log(ppl);
});
我正在尝试从资产文件夹中的 json 文件中获取数据。但反而出现错误。
heroes.service.ts
getPeopleData(): Observable<people[]>{
return this.http.get<people[]>('assets/data/someData.json');
}
people.interface.ts
export interface people{
id: number,
name: string,
age: number
}
someData.json:
[
{"id":1, "name": "Jack", "age": 21},
{"id":2, "name": "Rina", "age": 29},
{"id":3, "name": "Jonathan", "age": 42}
]
about.component.ts
peopleData:any;
ngOnInit() {
this.getPeople();
}
getPeople(): void {
this.heroesService.getPeopleData()
.subscribe(data => this.peopleData = data)
}
错误:
谁能帮帮我,问题出在哪里?相反,控制台中没有错误。
这对我有用:
服务:
public testJson(): Observable<people[]>{
return this.http.get("https://api.myjson.com/bins/1f5zag").map(res => res.json());
}
(你可以用那个url实际试试)
component.onInit
this.yourService.testJson()
.subscribe( evt => {
const ppl:people[] = evt;
console.log(ppl);
});