main.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const detailsTemplate = `<div>
  2. <h2>Details</h2>
  3. <ul>
  4. <li v-for="detail in details">{{ detail }}</li>
  5. </ul>
  6. </div>`;
  7. const productTemplate = `<div class="product">
  8. <div class="product-image">
  9. <a :href="link">
  10. <!-- bind the src attribute to the "image" property -->
  11. <img
  12. v-bind:src="image"
  13. :alt="description" :title="description"
  14. /><!-- :attr is shortcut for v-bind:attr -->
  15. </a>
  16. </div>
  17. <div class="product-info">
  18. <h1 v-cloak>{{ title }}</h1>
  19. <h2>Available?</h2>
  20. <p v-cloak v-show="inStock">In stock</p>
  21. <p v-cloak v-show="!inStock">Out of stock</p>
  22. <p>Shipping: {{ shipping }}</p>
  23. <product-details :details="details"></product-details>
  24. <h2>Colors</h2>
  25. <ul>
  26. <li v-for="(variant, index) in variants"
  27. v-bind:key="variant.variantId"
  28. @mouseover="updateImage(index)"
  29. class="color-box"
  30. :style="{ backgroundColor: variant.variantColor }"
  31. /><!-- @ est le raccourci de v-on: -->
  32. </ul>
  33. <h2>Sizes</h2>
  34. <ul>
  35. <li v-for="size in sizes" :key="size">{{ size }}</li>
  36. </ul>
  37. <button v-on:click="addToCart"
  38. :disabled="!inStock"
  39. :class="{ disabledButton: !inStock }"
  40. >Add to cart</button>
  41. <button v-on:click="removeFromCart">Remove</button>
  42. </div>
  43. </div>
  44. `;
  45. Vue.component("product-details", {
  46. props: {
  47. details: {
  48. type: Array,
  49. required: true,
  50. },
  51. },
  52. template: detailsTemplate,
  53. });
  54. Vue.component("product", {
  55. props: {
  56. isPremium: {
  57. type: Boolean,
  58. required: true,
  59. }
  60. },
  61. template: productTemplate,
  62. // Computer properties are cached until dependencies change.
  63. // => cheaper than methods.
  64. computed: {
  65. image() {
  66. const image = this.variants[this.selectedVariant].variantImage;
  67. return image;
  68. },
  69. inStock() {
  70. return this.variants[this.selectedVariant].variantQuantity > 0;
  71. },
  72. shipping() {
  73. if (this.isPremium) {
  74. return "Free";
  75. }
  76. return "2.99 €";
  77. },
  78. title() {
  79. return [this.brand, this.product].join(' ');
  80. }
  81. },
  82. data() {
  83. return {
  84. brand: "Vue Mastery",
  85. description: "Chaussettes montantes",
  86. details: [
  87. "80% cotton",
  88. "20% polyester",
  89. "Gender-neutral",
  90. ],
  91. inventory: 15,
  92. link: "https://www.google.com/search?q=vuejs+socks",
  93. product: "Socks",
  94. selectedVariant: 0,
  95. sizes: [34, 36, 38, 40, 42, 44, 46],
  96. variants: [
  97. {
  98. variantId: 2234,
  99. variantColor: "green",
  100. variantImage: "./assets/img/vmSocks-green-onWhite.jpg",
  101. variantQuantity: 10,
  102. },
  103. {
  104. variantId: 2235,
  105. variantColor: "blue",
  106. variantImage: "./assets/img/vmSocks-blue-onWhite.png",
  107. variantQuantity: 0,
  108. },
  109. ]
  110. }
  111. },
  112. methods: {
  113. addToCart() {
  114. this.variants[this.selectedVariant].variantQuantity--;
  115. this.$emit("add-to-cart", this.variants[this.selectedVariant].variantId);
  116. },
  117. removeFromCart() {
  118. this.variants[this.selectedVariant].variantQuantity++;
  119. this.$emit("remove-from-cart", this.variants[this.selectedVariant].variantId);
  120. },
  121. updateImage(index) {
  122. this.selectedVariant = index;
  123. }
  124. },
  125. });
  126. const app = new Vue({
  127. el: "#app",
  128. data: {
  129. cart: [],
  130. isPremium: false,
  131. },
  132. methods: {
  133. removeItem(id) {
  134. console.log("removeItem", id);
  135. const index = this.cart.indexOf(id);
  136. if (index > -1) {
  137. this.cart.splice(index, 1);
  138. }
  139. },
  140. updateCart(id) {
  141. console.log("updateCart", id);
  142. this.cart.push(id);
  143. }
  144. }
  145. });