js push一个对象,修改后会改变源对象的值?

<div style="margin: 10px;">
    <el-input v-model="item.name" v-for="item in ruleForm"></el-input>
    <el-button @click="submit()">添加</el-button>
    {{ruleForm}}
</div>
submit(){
    this.ruleForm.push(this.ruleForm[0]);
}

这样写会导致一个结果,两边公用一个数据源
image.png

然后换了种写法,但貌似还不行,有好的解决方案吗

submit(){
    this.ruleForm.push({});
}

image.png

阅读 3.3k
3 个回答
this.ruleForm.push(Object.assign({},this.ruleForm[0]));

如果只有一层可以这么写 `
this.ruleForm.push(JSON.parse(JSON.stringify(this.ruleForm[0])));
`~~~~

this.ruleForm.push({type: '', name: ''})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题