vue.js - vuejs2.0 提示Cannot read property 'push' of undefined
本文介绍了vue.js - vuejs2.0 提示Cannot read property 'push' of undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
new Vue({
el: '#swiperBanner',
data: {
slideList : []
},
created: function () {
this.getSlider();
},
methods: {
getSlider : function () {
$.getJSON(basePath+'slider/index.html',{'id':13}, this.getData);
},
getData : function (data) {
if(data.code==1){
if(!common.isNullOrEmpty(data.info.list)){
console.log(this.slideList);
$.each(data.info.list, function(i,item){
this.slideList.push(item);
});
//this.slideList = data.info.list;
}
}
}
},
updated: function () {
new Swiper('.swiper-banner', {
pagination: '.swiper-pagination',
paginationClickable: true
});
}
});
这是一段正常的读取数据的代码,为什么在填充数据的时候会提示Cannot read property 'push' of undefined,为什么没有push属性呢?
解决方案
this指向的问题,你在each的函数里面,当前的this并不是指向的vue的,所以没有找到slideList这个变量,自然就没有push这个属性了
getData : function (data) {
var _this = this;
if(data.code==1){
if(!common.isNullOrEmpty(data.info.list)){
console.log(this.slideList);
$.each(data.info.list, function(i,item){
_this.slideList.push(item);
});
//this.slideList = data.info.list;
}
}
}
这篇关于vue.js - vuejs2.0 提示Cannot read property 'push' of undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文