【发布时间】:2018-09-30 07:11:35
【问题描述】:
我正在使用 Vuejs 和 Lodash,并且我有一个计算属性,它只是对集合的属性求和。我以前为此使用过_.sum(),但是当我将它与vuejs vm一起使用时,它似乎只是连接字符串[object Object]!
我发了jsfiddle with the following code:
new Vue({
el: "#app",
data: {
todos: [
{ text: "Learn JavaScript", time: 5 },
{ text: "Learn Vue", time: 10 },
]
},
computed: {
additup() { return _.sum(this.todos, todo => todo.time ) },
additup2() { return _.sum(this.todos, function (todo) {
return parseInt(todo.time);
})} },
additup3() {
var t = 0;
_.each(this.todos, function(todo) { t+=todo.time; });
return t;
}
}
})
输出是:
方法一给出:[object Object][object Object]
方法2给出:[object Object][object Object]
方法 3 给出:15
有没有办法修复_.sum()?或者一种解决我对它为什么不起作用的理解的方法;-)
【问题讨论】:
标签: javascript vue.js lodash