美文网首页
vue改变数组或者对象的某一项,页面视图没有渲染

vue改变数组或者对象的某一项,页面视图没有渲染

作者: 绿啊绿啊绿刺猬 | 来源:发表于2020-04-08 14:27 被阅读0次

1.改变对象的某个属性值

this.tableData是一个数组,this.tableData[index]是一个对象.如果直接修改:

this.tableData[index].editable = true

用上面的方法改变对象的一个属性editable,打印发现值已经改变了,但是视图并没有渲染出来,应该用:

this.$set(this.tableData[index], 'editable', true)
// this.$set(要改变的对象, '属性名', 属性值)

2.改变数组的某一项

this.tableData是一个数组,this.tableObj是一个对象.如果直接改变数组的某一项:

this.tableData[index]  = this.tableObj

也是视图层不会渲染出来.用下面两种方法都可:

this.$set(this.tableData, index, this.tableObj)
this.tableData.splice(index, 1, this.tableObj)

深拷贝:

this.tableObj = JSON.parse(JSON.stringify(row))

相关文章

网友评论

      本文标题:vue改变数组或者对象的某一项,页面视图没有渲染

      本文链接:https://www.haomeiwen.com/subject/lkltmhtx.html