vue.demo.html 900 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <link rel="stylesheet" type="text/css" href="bulma.css">
  2. <div id="app">
  3. <h1>库存信息</h1>
  4. <hr>
  5. <ul>
  6. <li v-for="product in products">
  7. {{ product.name }} -
  8. <input type="number" v-model.number="product.quantity" min="0">
  9. <span v-if="product.quantity === 0">
  10. 已经售罄
  11. </span>
  12. <button @click="product.quantity += 1">
  13. 增加库存
  14. </button>
  15. </li>
  16. </ul>
  17. <h2>库存总量:{{ totalQuantity }}台</h2>
  18. </div>
  19. <script src="https://cdn.jsdelivr.net/npm/vue"></script>
  20. <script>
  21. const app = new Vue({
  22. el: '#app',
  23. data: {
  24. products: [
  25. {"id": 1, "name": "iPhone X", "quantity": 20},
  26. {"id": 2, "name": "华为 Mate20", "quantity": 0},
  27. {"id": 3, "name": "小米 Mix3", "quantity": 50}
  28. ]
  29. },
  30. computed: {
  31. totalQuantity() {
  32. return this.products.reduce((sum, product) => {
  33. return sum + product.quantity
  34. }, 0);
  35. }
  36. }
  37. });
  38. </script>