Last active
January 12, 2019 13:54
-
-
Save c0depanda/be10939615767bca9f3b192b64a0a576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<main> | |
<h1>Cart Content</h1> | |
<p>Total Number of Items: {{totalNumberOfCartItems}}</p> | |
<form @submit.prevent="addItemToCart"> | |
<input type="text" v-model="item" required> | |
<button type="submit">Add to cart</button> | |
</form> | |
</main> | |
</template> | |
<script> | |
export default { | |
data() { | |
return { | |
item: '' | |
} | |
}, | |
computed: { | |
totalNumberOfCartItems() { | |
// Accessing the Vuex state | |
return this.$store.getters.totalNumberOfCartItems; | |
} | |
}, | |
methods: { | |
addItemToCart() { | |
// Check that the input field isn't empty | |
if(this.item !== '') { | |
// commiting the additemtocart mutation with the payload | |
this.$store.commit('addItemToCart', this.item) | |
} | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment