ProductList.vue 605 B

12345678910111213141516171819202122232425262728293031
  1. <template>
  2. <ul>
  3. <li
  4. v-for="product in products"
  5. :key="product.id">
  6. {{ product.title }} - {{ product.price | currency }}
  7. <br>
  8. <button
  9. :disabled="!product.inventory"
  10. @click="addProductToCart(product)">
  11. Add to cart
  12. </button>
  13. </li>
  14. </ul>
  15. </template>
  16. <script>
  17. import { mapState, mapActions } from 'vuex'
  18. export default {
  19. computed: mapState({
  20. products: state => state.products.all
  21. }),
  22. methods: mapActions('cart', [
  23. 'addProductToCart'
  24. ]),
  25. created () {
  26. this.$store.dispatch('products/getAllProducts')
  27. }
  28. }
  29. </script>