object is not extensible 错误在vuejs

object is not extensible error in vuejs

我想用名为 colspan 的新字段更新促销对象。但是我运气不好,得到

错误:

未捕获(承诺)TypeError:无法添加 属性 colspan,对象不可扩展 在评估(评估在(http://localhost:8080/app.js:3160:1),:71:32) 在 Array.forEach(本地) 在评估(评估在(http://localhost:8080/app.js:3160:1),:66:35) 在 Array.forEach(本地) 在评估(评估在(http://localhost:8080/app.js:3160:1),:64:47) 在 Array.forEach(本地) 在评估(评估在(http://localhost:8080/app.js:3160:1),:62:23) 在承诺 () 在 F(在(http://localhost:8080/app.js:865:1)评估,:35:28) 在 Object.modifyData(在(http://localhost:8080/app.js:3160:1)评估,:48:12)

departmentIds.forEach(departmentId => {
          results[departmentId] = []
          departmentWiseResults[departmentId].forEach((promo, index) => {
            let tmpPromo = promo
            dateRanges.dateRanges.forEach(range => {
              let startedDateWeek = moment(promo.startDate).week()
              let endDateWeek = moment(promo.endDate).week()
              let startedYear = moment(promo.startDate).year()
              let endedYear = moment(promo.endDate).year()
              tmpPromo.colspan = 0
              if (range.startYear === startedYear && range.endYear === endedYear && range.weekNumber <= endDateWeek && range.weekNumber >= startedDateWeek) {
                tmpPromo.colspan++
              }
              departmentWiseResults[departmentId].splice(index, 1, tmpPromo)
              console.log('stareted:', startedDateWeek, endDateWeek, startedYear, endedYear, promo, tmpPromo, departmentWiseResults[departmentId])
            })
            console.log('promo after adding colspna:', promo)
            // if (isInRange(range, promo)) {
            //   console.log('for range', range.startDate, range.endDate)
            //   console.log('for promo', promo.id)
            //   console.log(
            //     'diff is',
            //     findWeekspan(range, dateRanges.dateRanges[dateRanges.dateRanges.length - 1], promo)
            //   )
            // // if (!promo.used) {
            // //   promo.used = true
            // //   results[departmentId]
            // // }
            // }
          })
        })

请帮我解决这个问题。

对象 promo 已通过 Object.preventExtensions (or seal or freeze). That means you cannot add new properties to it. Instead, you can make a new copy of it with new properties. A convenient way to do this is with Object.assign 关闭了可扩展性。

let tmpPromo = Object.assign({colspan: 0}, promo);

这从定义 colspan 的匿名对象开始,将 promo 的属性复制到其中,然后 returns 对象(我们分配给 tmpPromo) .