1
我想将函数用作数据属性。这似乎在“作品”数据属性的情况下正常工作。不过,我需要这个函数中的上下文,以便我可以计算存储在this.shoppingCart(另一个属性)中的值。将vuejs函数作为具有上下文的数据绑定vuejs
这可能吗?如果是这样我做错了什么?
new Vue({
el: '#vueApp',
data: {
shoppingCart: [],
works : function() {
return "testfunc";
},
totalPriceCalcProperty : function() {
this.totalPrice = this.shoppingCart.reduce(function(total, cartItem){
console.log(total, cartItem);
return total + parseFloat(cartItem.price);
}, 0);
}
},
methods: {
totalPriceCalc: function() {
this.totalPrice = this.shoppingCart.reduce(function(total, cartItem){
console.log(total, cartItem);
return total + parseFloat(cartItem.price);
}, 0);
},
}