• 8.由于垃圾评论太多,关闭网站评论功能

  • 7.暂时删除 收藏夹  -2020年7月6日20:01:09

  • 6.添加日历小工具

  • 5.工具箱做为单独页面

  • 4.工具箱新增常用工具链接

  • 3.关闭网站内链–20200626

  • 2.代码风格改为 暗色主题–20200626

     

  • 1.取消左侧菜单列表显示 –20200625

  • 网站升级为https

    2020年6月25日21:40:52

vue 把对象{}push进数组的问题

APP开发 易天法地 4年前 (2020-08-30) 1621次浏览 扫描二维码

找到了问题所在 做下记录 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);

本站文章如无特殊说明均为原创
文章标题: vue 把对象{}push进数组的问题
转载请注明原文链接:https://www.zylearning.top/915.html
喜欢 (0)