Cart.vue 919 B

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