我在后端(JSON)显示答案时遇到了一些问题。这是我服务器的json:
{
"items": [
{
"countryId": 1,
"countryName": "Особь без подданства",
"countryIdEDBO": -1,
"countryIsArchive": 0
},
{
"countryId": 2,
"countryName": "Австрал",
"countryIdEDBO": 36,
"countryIsArchive": 0
}
],
"_links": {
"self": {
"href": "http://...."
},
"next": {
"href": "http://...."
},
"last": {
"href": "http://...."
}
},
"_meta": {
"totalCount": 247,
"pageCount": 13,
"currentPage": 1,
"perPage": 20
},
}
在从后端显示json之前,我们必须在Angular 5/6中创建这个json的模型。使用服务http://jvilk.com/MakeTypes/生成JSON模型并在我的代码中插入。 这是JSON的模型:
export interface Country {
items?: (ItemsEntity)[] | null;
_links: Links;
_meta: Meta;
}
export interface ItemsEntity {
countryId: number;
countryName: string;
countryIdEDBO: number;
countryIsArchive: number;
}
export interface Links {
self: SelfOrNextOrLast;
next: SelfOrNextOrLast;
last: SelfOrNextOrLast;
}
export interface SelfOrNextOrLast {
href: string;
}
export interface Meta {
totalCount: number;
pageCount: number;
currentPage: number;
perPage: number;
}

这是我在组件中的代码:
export class CountryComponent implements OnInit {
constructor(private _preloadService: PreloadService, private countryService:CountryService) { }
country:Country;
ngOnInit() {
//пока что undefined возвращает в консоль вывода
this.getListCountry();
console.log(this.country);
}
getListCountry(){
this.countryService.getCountryList().subscribe((data:Country)=>(this.country=data));
}

因此,浏览器控制台中的答案是" Undefined"。我应该怎么办?我知道,模型中的问题,所以也许你知道链接或回答我必须如何从服务器为我的JSON答案创建模型?
答案 0 :(得分:0)
只需将“console.log()”放入从v服务获取答案的方法
export class CountryComponent implements OnInit {
constructor(private _preloadService: PreloadService, private countryService:CountryService) { }
country:Country;
ngOnInit() {
//пока что undefined возвращает в консоль вывода
this.getListCountry();
}
getListCountry(){
this.countryService.getCountryList().subscribe((data:Country)=>(console.log(data)));
}
}