最近在項目中遇到一個這樣的小問題,在用數組方法結合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