vue 通过 this.list_data[index].discount_price = e 赋值为什么报错说没有先定义?

this.goods_list_data[index].discount_price = e

我有一个 goods_list_data 的数组对象如下图

vue 通过 this.list_data[index].discount_price = e 赋值为什么报错说没有先定义?

里面这个 discount_price 是不存在,是我通过 this.goods_list_data[index].discount_price = e 自行赋值上去的,这就导致报了个 vue 的错误,说我没先定义,就直接赋值使用,

但是我没办法去先定义,因为这个 goods_list_data 数组对象的数据,是接口返回过来的,而且我又要对应索引去赋值,所以只能这样做,不知道大家平时有没有遇到过这个问题,一般怎么处理的?


回答:

https://v2.cn.vuejs.org/v2/ap...

vm.$set( target, propertyName/index, value )

参数:

{Object | Array} target

{string | number} propertyName/index

{any} value

返回值:设置的值。

用法:

这是全局 Vue.set 的别名。

参考:Vue.set


回答:

可以使用 $set 去添加对象的新属性。。
也可以在接收服务器返回数据的时候使用 map 去添加一些没有的属性例如说

fetch(params).then(res=>{

this.data = res.map(item=>({...item, custProp:null}))

})

这样来添加一些属性。

相关阅读:
Vue.set | API — Vue.js


回答:

chrome 打印的是一个动态数据,可能是报错的时候,该变量是没赋值的,过了一段时间后有值了,log 的数据正常打印出来,你以为一直有值

以上是 vue 通过 this.list_data[index].discount_price = e 赋值为什么报错说没有先定义? 的全部内容, 来源链接: utcz.com/p/932949.html

回到顶部