VUE中给JSON加个参数

it2024-10-12  35

我们拿到产品列表的JSON文件后。 JSON中没有count(数量)这个参数。 所以我们需要在代码上给JSON加上 count 参数。 上源码: vue.set创建count属性

add(){ this.number++; this.$emit('data',this.number) //在json中是没有count这个属性的这;在这里我们需要用到vue中提供的方法vue.set创建count属性 if(!this.food.count){ this.$set(this.food,"count",1) //在this.food中创建count属性;默认值为1 //如果直接使用众所周知,直接给food赋值操作,虽然可以新增属性,但是不会触发视图更新 // this.food.count = 1 //如果不引入全局也可以这样写 //this.$set(this.food,"count", 1) }else{ this.food.count++; } },
最新回复(0)