2017-08-15 111 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); 
     }); 
    } 
相關問題