关于在vue中结合数组方法的this的指向问题

最近在项目中遇到一个这样的小问题,在用数组方法结合vue的时候,this的指向不是当前的vue实例,直接看代码

allSelect: function() {
   var _this = this;
   console.log(_this)
   this.checkId = [];
   this.batchDealItem.forEach(function(item) {
       //console.log(item.APPID)
      _this.checkId.push(item.APPID);//指向vue

     console.log(this)//指向window
   })
},

注意这里this的变化,在遍历的时候,this是指向window的,需要在遍历之前定义this的指向,不然浏览器会报错

大叔的文章:http://www.cnblogs.com/TomXu/archive/2012/01/17/2310479.html

原文地址:https://www.cnblogs.com/yanzai/p/6590656.html