i am building a simple cart using Vuejs. I am having trouble calculating the total in the cart. It seems not to be adding the price together but displaying the numbers together like if i have two items in the cart at prices 10 and 15 it displays "1015" where the answer should be "25"
Cart.Vue
<template>
<div class="container">
<h1>Your Cart</h1>
<div class="center" v-for="item in shoppingCart">
<div class="col-md-8 cart-item">
<div class="row">
<div class="item-img pull-left">
<img class="media-object" v-bind:src="item.thumbnailUrl">
</div>
<div class="item-info pull-right">
<h5>{{item.title}}</h5>
<h5>{{item.price}}</h5>
<button class="btn btn-danger" v-on:click="removeFromCart(item.id)">Remove From Cart</button>
</div>
</div>
</div>
</div>
<div class="total">
<h3> Total : €{{total}} </h3>
</div>
</div>
</template>
<script>
export default {
name: 'Cart',
props: ['shoppingCart'],
data() {
return {
}
},
computed: {
total() {
return this.shoppingCart.reduce((acc, item) => acc + item.price, 0);
}
},
methods: {
removeFromCart: function(productId) {
console.log('remove product from cart', productId);
this.$emit('removeFromCart', productId);
}
}
}
</script>
Sample of what it looks like on the web
Still only very new to Vuejs, so any feedback would be great, thanks!