所以我從我的休息服務中獲取資料,Dto 已填充,但是當我嘗試訪問屬性時,它是未定義的。
在這里,我正在獲取資料
this.mapservice.getAllCoordinates().subscribe((data: CoordinateDto[]) => {
this.coordinates = data;
this.parseMapCoordinates();
this.initLocation();
});
在這里,我嘗試將其決議為一個數字,以便我可以用它填充我的其他物件。但是屬性都是未定義的。
parseMapCoordinates() {
let newCoords: MapCoordinates;
this.coordinates.forEach((coords) => {
console.log(coords);
console.log(coords.user_Id);
console.log(coords.date);
console.log(coords.koordinate);
newCoords.latitude = parseFloat(coords.koordinate.split(', ')[0]);
newCoords.longitude = parseFloat(coords.koordinate.split(', ')[1]);
this.mapCoordinates.push(newCoords);
});
}
當我將滑鼠懸停在坐標上時,它會向我顯示值,但是當我將滑鼠懸停在坐標上時,它會顯示“未定義”,我不知道為什么。
這是日志
最后是 Dto
export interface CoordinateDto{
koordinate: string;
date: Date;
user_Id: number;
感謝您的幫助 :)
uj5u.com熱心網友回復:
this.coordinates是一個專案陣列。所以你需要用它的索引訪問它,例如:
parseMapCoordinates() {
let newCoords: MapCoordinates;
for (let i = 0; i < this.coordinates.length; i ) {
const coords = this.coordinates[i];
console.log(coords);
console.log(coords.user_Id);
console.log(coords.date);
console.log(coords.koordinate);
newCoords.latitude = parseFloat(coords.koordinate.split(', ')[0]);
newCoords.longitude = parseFloat(coords.koordinate.split(', ')[1]);
this.mapCoordinates.push(newCoords);
}
}
uj5u.com熱心網友回復:
將介面更改為類。介面沒有初始資料,它大多只定義型別:
export interface CoordinateDto{
koordinate: string;
date: Date;
user_Id: number;
}
到
export class CoordinateDto {
koordinate: string;
date: Date;
user_Id: number;
constructor() {
this.koordinate = '';
this.date = new Date();
this.user_Id = 0;
}
}
之后,在你的parseMapCoordinates()函式中
而不是let newCoords: MapCoordinates;
改為let newCoords = new CoordinateDto();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/322408.html
標籤:有角的
下一篇:使用過濾器與切片陣列方法不作業
