在我的vue应用程序中
data: {
emailData: JSON.parse('#{{@mail.data}}')
},
computed: {
emailJson: function () {
return JSON.stringify(this.emailData);
}
},
methods: {
addBlock: function (type) {
this.emailData.elements.push({type: type, data: ''})
},
removeBlock: function (index) {
this.emailData.elements.splice(index, 1)
},
moveBlock: function (direction, index) {
if (direction === 'up' && index > 0) {
let temp = this.emailData.elements[index - 1]
this.emailData.elements[index - 1] = this.emailData.elements[index]
this.emailData.elements[index] = temp
} else if (direction === 'down' && index < this.emailData.elements.length) {
let temp = this.emailData.elements[index + 1]
this.emailData.elements[index + 1] = this.emailData.elements[index]
this.emailData.elements[index] = temp
}
}
}
如果我跑了
moveBlock('up',2)
我可以看到数据
emailData
emailJson
仍然显示修改前的数据。如果我以后打电话
addBlock
前两者的变化
moveBlock
添加块
电子邮件JSON
.
添加块
和
removeBlock
移动块