组件:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-ore-table',
templateUrl: './ore-table.component.html',
styleUrls: ['./ore-table.component.less']
})
export class OreTableComponent implements OnInit {
ores = '../assets/json/ores.json';
prices = 'https://esi.tech.ccp.is/latest/markets/prices/?datasource=tranquility';
oreArray: any;
pricesArray: any;
joinedArray: any;
constructor(private http: HttpClient) { }
getOres() {
this.http.get(this.ores).subscribe(data => {
this.oreArray = data;
this.getPrices();
});
}
getPrices() {
this.http.get(this.prices).subscribe(data => {
this.pricesArray = data;
this.joinPrices();
});
}
joinPrices() {
this.oreArray.forEach(function(data) {
const matchingPrice = this.getMatchingPrice(data);
});
}
getMatchingPrice(data) {
for (let i = 0; i < this.pricesArray.length; i++) {
if (this.pricesArray[i].type_id === data.id) {
return this.pricesArray[i];
}
}
return false;
}
ngOnInit() {
this.getOres();
}
}
不确定这里发生了什么。我将这个工作代码从vanilla JS转移到Angular 2 / Typescript,并在尝试运行上述内容时出现此错误:
Cannot read property 'getMatchingPrice' of undefined
此行发生错误:
const matchingPrice = this.getMatchingPrice(data);
任何见解都表示赞赏。谢谢。
答案 0 :(得分:2)
请改为尝试:
joinPrices() {
this.oreArray.forEach((data) => {
const matchingPrice = this.getMatchingPrice(data);
});
}