2017-08-15 110 views
-1

组件:角2无法读取未定义的属性(函数名)

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(); 
    } 
} 

不知道这到底是怎么回事。我把从香草JS这个工作代码角2 /打字稿及收到此错误试图运行时,上面:

Cannot read property 'getMatchingPrice' of undefined 

的错误是在这条线出现:

const matchingPrice = this.getMatchingPrice(data); 

任何有识之士非常感谢。谢谢。

+2

使用,而不是'function'箭头语法。您可以在链接中了解有关此主题的更多信息。 – echonax

回答

2

试试这个:

joinPrices() { 
     this.oreArray.forEach((data) => { 
     const matchingPrice = this.getMatchingPrice(data); 
     }); 
    } 
相关问题