1.问题:后台传过来数据——weather一连窜字符串
深克隆: 创建一个新对象,属性中引用的其他对象也会被克隆,不再指向原有对象地址。 JSON.parse、JSON.stringify()
JSON.parse()方法将数据转换为 JavaScript 对象。
JSON.stringify()方法用于将 JavaScript 值转换为 JSON 字符串
2.解决方案: JSON.parse(text[, reviver])
- text:必需, 一个有效的 JSON 字符串。
- reviver: 可选,一个转换结果的函数, 将为对象的每个成员调用此函数。
js:
ajaxApi.FarmingRecord.list({"plant_id":this.plan_batch_id}).then(res => {
console.log(res.data)
if(res.code == 0){
var timers = []
this.plantData = [];
for(let i = 0;i<res.data.length;i++){
let item = res.data[i];
let weather = JSON.parse(item.weather);
item['weatherJson']=weather;
this.plantData.push(item);
}
}else {
this.$message.error(res.msg)
}
});
},
转换后结果:
html:
<div class="des" v-for="(item,i) in plantData" :key="i">
<span>{{item.weatherJson.weather}}</span>
</div>