我遇到的问题是访问Month对象数组中的变量。
接口:
export class Month {
constructor(
public id: number, public month: string, public m50: any
) {}
}
服务:
m50_colour_april = [];
constructor(public db: AngularFireDatabase,) {
this.db.list('sentiment/april').valueChanges().subscribe((colours: any) => {
var m50: [0, 1];
m50 = colours[2];
this.m50_colour_april = m50[Object.keys(m50)[0]];
})
}
Months = [
new Month(1, 'May', this.m50_colour_april),
]
组件:
this.months = this._route.paramMap.pipe(
switchMap((params: ParamMap) =>
this._plot.getMonth(params.get('id')))
);
this.months.subscribe(locations => { console.log(locations) });
当尝试调用变量“ m50_colour_april”时,我收到一个未定义的数组,而我希望从我的Firebase API中获得调用。将结果记录到服务中构造函数的范围内时,此方法可以很好地工作,但是由于某些奇怪的原因,当将结果放入Months = []数组中时,它不起作用。
output: Month {id: 1, month: "May", m50: Array(0)}
expected output: Month {id: 1, month: "May", m50: "yellow"}