找到了问题所在 做下记录 https://blog.csdn.net/Lon_wang/article/details/96101617
在使用vue框架开发时,遇到一个数组push一条对象而导致之前push进去的对象也变成后面进去对象的值。后来发现是因为push对象时,指针一直指向的是之前输入框绑定的对象地址,所以输入框下次输入时值改变,数组内的值也就变了。
remark_formInline: {
name: '',
type: 'IT反馈',
creator: 'admin',
time: '' ,
account:''
}
this.remark_datas.push(this.remark_formInline);
//按上面这种写法直接push进去,数组内的值会跟着变化。
//按上面这种写法直接push进去,数组内的值会跟着变化。
需要重新申请一个内存空间,然后赋值给他,再push,实现深拷贝。
//data 部分
data() {
return {
remark_datas:[],
remark_formInline:{
id:'',
name: '',
type: '',
creator: '',
time: '',
account: '',
},
}
},
//下面为实现部分
//这里赋值 后面类推
this.remark_formInline.name = name ;
//下面push
let obj={
id: Object.keys(this.addDirectoryList.directoryRemark.remark_datas),
name: this.remark_formInline.name,
type: this.remark_formInline.type,
creator: this.remark_formInline.creator,
time: this.remark_formInline.time,
account: this.remark_formInline.account,
}
this.remark_datas.push(obj);