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);
},
}