Vue TypeError: Cannot set property ‘text‘ of undefined

叁歲伎倆 2022-12-20 06:08 343阅读 0赞

遇到这个问题

  1. TypeError: Cannot set property 'text' of undefined

首先在data定义的数据

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

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

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

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

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

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

发表评论

表情:
评论列表 (有 0 条评论,343人围观)

还没有评论,来说两句吧...

相关阅读