草庐IT

testVariable

全部标签

javascript - typescript 如何在foreach循环中访问组件变量?

嘿,有人能告诉我如何在foreach循环中访问组件变量吗?这是我的PlunkerpublictestVariable:number;test(){console.log('fired');varx=[1,2,3,4];x.forEach(function(e){this.testVariable=e;})console.log(this.testVariable);} 最佳答案 如果你使用function(e),它里面的this会引用函数的作用域而不是类。改用ArrowFunction(或FatArrow):x.forEach((e