-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
89 lines (85 loc) · 2.54 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Vue.component('product', {
props: {
premium: {
type: Boolean,
required: true,
}
},
template: `
<div class="product">
<div class="product-image">
<img :src="image" alt="VueSocks">
</div>
<div class="product-info">
<h1>{{ title }}</h1>
<p v-if="inStock">In Stock</p>
<p v-else>Out of Stock</p>
<p>Shipping: {{ shipping }}</p>
<ul>
<li v-for="detail in details">{{detail}}</li>
</ul>
<div class="color-box" v-for="(variant, index) in variants" :key="variant.variantId"
:style="{ backgroundColor: variant.variantColor }"
@mouseover="updateProductImage(index)">
</div>
<button v-on:click="addToCart" :disable="!inStock" :class="{ disabledButton: !inStock }">
Add to Cart
</button>
</div>
</div>
`,
data() {
return {
product: 'Socks',
brand: 'Vue Mastery ',
selectedVariant: 0,
details: ["80% cotton", "20% polyester", "Gender-neutral"],
variants: [
{
variantId: 2234,
variantColor: "green",
variantImage: './assets/vmSocks-green-onWhite.jpg',
variantQuantity: 10
},
{
variantId: 2235,
variantColor: "blue",
variantImage: './assets/vmSocks-blue-onWhite.jpg',
variantQuantity: 0,
}
],
}
},
methods: {
addToCart: function () { this.$emit('add-to-cart', this.variants[this.selectedVariant].variantId) },
updateProductImage: function (index) {
this.selectedVariant = index;
}
},
computed: {
title() {
return this.brand + this.product;
},
image() {
return this.variants[this.selectedVariant].variantImage;
},
inStock() {
return this.variants[this.selectedVariant].variantQuantity;
},
shipping() {
return this.premium ? "Free" : "2.99";
}
}
})
let app = new Vue({
el: '#app',
data: {
premium: false,
cart: [],
},
methods: {
updateCart: function (id) {
this.cart.push(id);
}
}
})