Vue TypeError: Cannot set property ‘text‘ of undefined

  • Post author:
  • Post category:vue


遇到这个问题

TypeError: Cannot set property 'text' of undefined

首先在data定义的数据

filterData:[
			   [{ text: '全部状态', value: '' }],
			   [{ text: '全部类型', value: '' }, { text: '类型1', value: 1 }, { text: '类型2', value: 2 }, { text: '类型3', value: 3 }]
		   ],

想在filterData[0]后继续追加{ text: ’ ‘, value: ’ ’ }数据

一开始写的错误代码(好睿智)

for(let i = 0;i < that.bcList.length;i ++) {
	that.filterData[0][i+1].text = that.bcList[i].className + '(' + that.bcList[i].startTime + '-' + that.bcList[i].endTime + ')',
	that.filterData[0][i+1].value = that.bcList[i].id
	}
}


因为这个时候并没有定义i+1位置的text和value

改正后:

for(let i = 0;i < that.bcList.length;i ++) {
	that.filterData[0][i+1] = {
		text: that.bcList[i].className + '(' + that.bcList[i].startTime + '-' + that.bcList[i].endTime + ')',
	    value: that.bcList[i].id
	}
}



版权声明:本文为ka_xingl原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。