我需要将计算值保存在我的数据库中,但似乎无法通过下面的示例访问它:
computed: {
total: {
get: function() {
return this.items.reduce(
(acc, item) => acc + (item.price * item.quantity) * (1 - item.discount/100), 0
)
},
set: function(newValue) {
console.log(newValue);
// this.tototata = newValue;
}
}
},在模板中,计算值工作正常,但控制台中未显示任何内容
我正在使用vue 2.6.11
这是最好的方法吗?我应该使用方法吗?
发布于 2020-01-26 17:48:29
我认为computed setter是在手动设置计算值时调用的。例如,如果您执行类似于this.total = newTotal的操作,则将触发设置器。为了在每次更新时将计算值保存在数据库中,您可能需要设置一个监视器:
computed: {
total: {
get: function() {
return this.items.reduce(
(acc, item) => acc + (item.price * item.quantity) * (1 - item.discount / 100), 0
)
}
}
},
watch: {
total(newValue) {
// Save to database
}
}你可以在这里阅读更多关于Computed Setter的内容。希望这能帮助你解决这个问题。
https://stackoverflow.com/questions/59916902
复制相似问题