main.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. </div>
  42. </div>
  43. `;
  44. Vue.component("product-details", {
  45. props: {
  46. details: {
  47. type: Array,
  48. required: true,
  49. },
  50. },
  51. template: detailsTemplate,
  52. });
  53. Vue.component("product", {
  54. props: {
  55. isPremium: {
  56. type: Boolean,
  57. required: true,
  58. }
  59. },
  60. template: productTemplate,
  61. // Computer properties are cached until dependencies change.
  62. // => cheaper than methods.
  63. computed: {
  64. image() {
  65. const image = this.variants[this.selectedVariant].variantImage;
  66. return image;
  67. },
  68. inStock() {
  69. return this.variants[this.selectedVariant].variantQuantity > 0;
  70. },
  71. shipping() {
  72. if (this.isPremium) {
  73. return "Free";
  74. }
  75. return "2.99 €";
  76. },
  77. title() {
  78. return [this.brand, this.product].join(' ');
  79. }
  80. },
  81. data() {
  82. return {
  83. brand: "Vue Mastery",
  84. description: "Chaussettes montantes",
  85. details: [
  86. "80% cotton",
  87. "20% polyester",
  88. "Gender-neutral",
  89. ],
  90. inventory: 15,
  91. link: "https://www.google.com/search?q=vuejs+socks",
  92. product: "Socks",
  93. selectedVariant: 0,
  94. sizes: [34, 36, 38, 40, 42, 44, 46],
  95. variants: [
  96. {
  97. variantId: 2234,
  98. variantColor: "green",
  99. variantImage: "./assets/img/vmSocks-green-onWhite.jpg",
  100. variantQuantity: 10,
  101. },
  102. {
  103. variantId: 2235,
  104. variantColor: "blue",
  105. variantImage: "./assets/img/vmSocks-blue-onWhite.png",
  106. variantQuantity: 0,
  107. },
  108. ]
  109. }
  110. },
  111. methods: {
  112. addToCart(mouseEvent) {
  113. this.variants[this.selectedVariant].variantQuantity--;
  114. this.$emit("add-to-cart", this.variants[this.selectedVariant].variantId);
  115. },
  116. updateImage(index) {
  117. this.selectedVariant = index;
  118. }
  119. },
  120. });
  121. const app = new Vue({
  122. el: "#app",
  123. data: {
  124. cart: [],
  125. isPremium: false,
  126. },
  127. methods: {
  128. updateCart(id) {
  129. console.log("uC", id);
  130. this.cart.push(id);
  131. }
  132. }
  133. });