我正在用vue.js
做一个简单的待办事项列表。我试图将所有price
总和在一个数组中。要做到这一点我写了一个小函数内部computed
, 但出了问题,这是我js
:Vue.js计算函数什么都不返回
var app= new Vue({
el: "#app",
data: {
message: "Lista della spesa",
seen: true,
todos: [
{msg: 'prezzemolo', price: 10},
{msg: 'pomodori', price: 20},
{msg: 'zucchine', price: 5}
],
},
methods: {
addToDo: function() {
if(this.nome && this.prezzo) {
this.todos.push({msg: this.nome, price: this.prezzo});
}
this.nome = "";
this.prezzo = "";
},
RemoveThis: function(index) {
this.todos.splice(index,1);
}
},
computed: {
totale: function() {
var total = 0;
this.todos.forEach(function() {
total += this.todos.price
});
return total;
}
}
});
存在被打破了功能forEach
里面的东西,任何人都知道为什么吗?
您是否尝试过一个简单的for循环看到的结果? –
是的,但我想看到forEach –