ShoppingCart.vue 978 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <template>
  2. <div class="cart">
  3. <h2>Your Cart</h2>
  4. <p v-show="!products.length">
  5. <i>Please add some products to cart.</i>
  6. </p>
  7. <ul>
  8. <li v-for="product in products" :key="product.id">
  9. {{ product.title }} - {{ currency(product.price) }} x {{ product.quantity }}
  10. </li>
  11. </ul>
  12. <p>Total: {{ currency(total) }}</p>
  13. <p><button :disabled="!products.length" @click="checkout(products)">Checkout</button></p>
  14. <p v-show="checkoutStatus">Checkout {{ checkoutStatus }}.</p>
  15. </div>
  16. </template>
  17. <script>
  18. import { mapGetters, mapState } from 'vuex'
  19. import { currency } from '../currency'
  20. export default {
  21. computed: {
  22. ...mapState({
  23. checkoutStatus: state => state.cart.checkoutStatus
  24. }),
  25. ...mapGetters('cart', {
  26. products: 'cartProducts',
  27. total: 'cartTotalPrice'
  28. })
  29. },
  30. methods: {
  31. currency,
  32. checkout (products) {
  33. this.$store.dispatch('cart/checkout', products)
  34. }
  35. }
  36. }
  37. </script>