【发布时间】:2018-03-17 06:30:57
【问题描述】:
使用 VueJS 并在其中导入 Lodash。当使用_.forEach等lodash函数时,函数体内的this指的是lodash实例。如何使 this 指向 Vue 组件的实例?
_.forEach(records, function(val)
{
if (this.max_records >0) // max_records is defined in data(), so this should be the cimponent instance
{
}
});
【问题讨论】:
-
谢谢thanksd,但可能不是。并且对于这个简单问题的解决方案可能过于深入。 @asemahle 的解决方案解决了它。
-
我相信这是完全相同的问题,而 asemahle 的解决方案正是那里接受的答案中的解决方案之一。我建议通读这篇文章,以更好地了解
this的范围一般是如何工作的。 -
谢谢,真的会看看。