|
1 | 1 | <html> |
2 | 2 | <head> |
3 | | - <script src="https://unpkg.com/vue"></script> |
4 | 3 | <link href="./assets/style.css" rel="stylesheet"> |
5 | 4 | <link href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" rel="stylesheet"> |
6 | 5 | <title>Reviewable Socks</title> |
|
17 | 16 | </div> |
18 | 17 | </div> |
19 | 18 |
|
20 | | - <a href="02-ComputedSocks.html" class="prev"> |
| 19 | + <a href="03-ComponentSocks.html" class="prev"> |
21 | 20 | <i class="fas fa-arrow-left"></i> moving back |
22 | 21 | </a> |
23 | 22 | </body> |
24 | 23 | </html> |
25 | 24 |
|
26 | 25 |
|
27 | | -<script type="text/javascript"> |
28 | | - const comp = Vue.component('product', { |
29 | | - // Props with validation |
30 | | - props: { |
31 | | - premium: {type: Boolean, required: true, default: false} |
32 | | - }, |
33 | | - // Without validation |
34 | | - // props: ['premium'], |
35 | | - template: ` |
36 | | - <div class="product"> |
37 | | - <div class="product-image"> |
38 | | - <img :src="image"> |
39 | | - </div> |
40 | | -
|
41 | | - <div class="product-info"> |
42 | | - <h1>{{ product }}</h1> |
43 | | -
|
44 | | - <div> |
45 | | - <p v-if="inventory > 10">In Stock</p> |
46 | | - <p v-else-if="inventory">Almost sold out</p> |
47 | | - <p v-else>Out of Stock</p> |
48 | | - </div> |
49 | | -
|
50 | | - <p v-if="premium">FREE Shipping</p> |
51 | | - <p v-else>Shipping: $4.99</p> |
52 | | -
|
53 | | - <button |
54 | | - v-on:click="addToCart" |
55 | | - :disabled="!inventory" |
56 | | - :class="inventory ? '' : 'disabledButton'" |
57 | | - > |
58 | | - Add to Cart |
59 | | - </button> |
60 | | -
|
61 | | - <div v-for="(variant, index) in variants" |
62 | | - :key="variant.id" |
63 | | - class="color-box" |
64 | | - :style="{backgroundColor: variant.color}" |
65 | | - @mouseover="selectedVariantIndex = index"> |
66 | | - </div> |
67 | | - </div> |
68 | | - </div> |
69 | | - `, |
70 | | - data() { |
71 | | - // Return a fresh object reference each time |
72 | | - return { |
73 | | - product: 'Socks', |
74 | | - selectedVariantIndex: 0, |
75 | | - variants: [ |
76 | | - {id: 1, color: 'green'}, |
77 | | - {id: 2, color: 'blue'} |
78 | | - ], |
79 | | - inventory: 3, |
80 | | - }; |
81 | | - }, |
82 | | - methods: { |
83 | | - addToCart() { |
84 | | - this.inventory--; |
85 | | - const selectedVariant = this.variants[this.selectedVariantIndex]; |
86 | | - this.$emit('add-to-cart', {product: this.product, variant: selectedVariant}); |
87 | | - } |
88 | | - }, |
89 | | - computed: { |
90 | | - image() { |
91 | | - const color = this.variants[this.selectedVariantIndex].color; |
92 | | - return `./assets/socks-${color}.jpg`; |
93 | | - } |
94 | | - } |
95 | | - }) |
96 | | - |
97 | | - const app = new Vue({ |
98 | | - el: '#app', |
99 | | - data: { |
100 | | - premium: true, |
101 | | - cart: [] |
102 | | - }, |
103 | | - methods: { |
104 | | - updateCart(product) { |
105 | | - console.log('Adding to cart:', product); |
106 | | - this.cart.push(product); |
107 | | - } |
108 | | - } |
109 | | - }); |
110 | | -</script> |
| 26 | +<script src="https://unpkg.com/vue"></script> |
| 27 | +<script src="04-ReviewableSocks.js"></script> |
111 | 28 |
|
112 | 29 |
|
113 | 30 | <!-- livereload --> |
|
0 commit comments