我有一个Vue.js应用程序,我在一系列项目上有一个v形重复.我想在项目列表中添加一个新项目.当我try this.items.push(this.newItem)
次时,推送的对象仍然绑定到输入.考虑下面的内容:
new Vue({
el: '#demo',
data: {
items: [
{
start: '12:15',
end: '13:15',
name: 'Whatch Vue.js Laracast',
description: 'Watched the Laracast series on Vue.js',
tags: ['learning', 'Vue.js', 'Laracast', 'PHP'],
note: "Vue.js is really awesome. Thanks Evan You!!!"
},
{
start: '13:15',
end: '13:30',
name: "Rubik's Cube",
description: "Play with my Rubik's Cube",
tags: ['Logic', 'Puzzle', "Rubik's Cube"],
note: "Learned a new algorithm."
}
],
newItem: {start: '', end: '', name: '', description: '', tags: '', note: ''}
},
methods: {
addItem: function(e) {
e.preventDefault();
this.items.push(this.newItem);
}
}
});
如上所述,将把绑定到items数组的对象推送到items数组上.问题是我只想要一个对象的副本,这样当输入改变时它就不会再改变了.看这个this fiddle.我知道我能做到:
addItem: function(e) {
e.preventDefault();
this.items.push({
name: this.newItem.name,
start: this.newItem.start,
end: this.newItem.end,
description: this.newItem.description,
tags: this.newItem.tags,
notes: this.newItem.notes
})
}
This works但这是很多重复.
The question:.是否有一种内置方法,可以只添加对象的副本,而不是持久对象.