我有一個相當可笑的問題。
來自訂閱的 observable 的物件在將其列印到控制臺時清楚地包含其中的資料,但在實際代碼中,打字稿表示其未定義。你自己看:
initData(): void {
this.backendService.getData().subscribe((depotDays: DepotDayAcc[]) => {
console.log(depotDays)
console.log(depotDays[0])
console.log(depotDays[0].investment)
console.log(depotDays[0]["day"])
console.log('depotDays[0] == undefined', depotDays[0] == undefined)
console.log('depotDays[0].investment == undefined', depotDays[0].investment == undefined)
})
}

constructor(
public day: Date,
public investment: number = -1,
public profit: number = 0,
public value: number = 0,
public percent: number = 0,
public tickers: Set<string> = new Set<string>()) {
}
uj5u.com熱心網友回復:
正如 ShamPooSham 在評論中指出的那樣,您的資料都是字串。你有一個字串陣列,而不是物件。
const depotDay = JSON.parse(depotDays[0]);
console.log(depotDay.investment);
選擇是修復有效負載以使其正確反序列化,或者自己反序列化每個條目。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/342734.html
上一篇:無法系結到“showQR”,因為它不是“div”的已知屬性?
下一篇:無法匯入外部NPM包來制作角度庫
