3 回答

TA贡献1845条经验 获得超8个赞
这是因为您使用forEach的方式。改用箭头功能,如下所示。并且建议在使用打字稿类时始终使用箭头功能,否则您将继续遇到此问题。
this.itemPrices.forEach((prices) =>{
if(prices.length != this.itemCount){
// handle error
}
});

TA贡献1797条经验 获得超4个赞
您可以使用ES6的箭头功能进行访问this。无论如何,请务必仔细阅读本文,以了解thisJavaScript的上下文以及在JS的OOP中使用它们的情况。
this.itemPrices.forEach((prices: number) => {
if (prices.length != this.itemCount){
// handle error
});
});

TA贡献1859条经验 获得超6个赞
this函数中的值与类中的值不同。您可以使用胖箭头功能,也可以使用bind(this)将该功能绑定到类。
this.itemPrices.forEach(function(prices){
if(prices.length != this.itemCount){
// handle error
}).bind(this);
添加回答
举报