File tree 3 files changed +34
-2
lines changed
3 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ <h1>{{ product }}</h1>
35
35
36
36
<!-- Events -->
37
37
< button
38
- v-on:click ="cart++; inventory-- "
38
+ v-on:click ="addToCart "
39
39
:disabled ="!inventory "
40
40
class ="add-to-cart "
41
41
:class ="inventory ? '' : 'disabledButton' "
Original file line number Diff line number Diff line change 28
28
Add to Cart
29
29
</button >
30
30
31
+ <button
32
+ @click =" removeFromCart"
33
+ :disabled =" cartLength === 0"
34
+ :class =" ['remove-from-cart', cartLength > 0 ? '' : 'disabledButton']"
35
+ >
36
+ Remove from cart
37
+ </button >
38
+
31
39
<div v-for =" (variant, index) in product.variants"
32
40
:key =" variant.id"
33
41
class =" color-box"
@@ -52,6 +60,7 @@ import ProductReview from './ProductReview.vue';
52
60
})
53
61
export default class Product extends Vue {
54
62
@Prop ({default: false }) private premium! : boolean ;
63
+ @Prop () private cartLength: number ;
55
64
56
65
product = {
57
66
name: " Vue Socks" ,
@@ -83,6 +92,12 @@ export default class Product extends Vue {
83
92
this .$emit (' add-to-cart' , {product: this .product , variant: selectedVariant });
84
93
}
85
94
95
+ removeFromCart() {
96
+ this .product .inventory ++ ;
97
+ const selectedVariant = this .product .variants [this .selectedVariantIndex ];
98
+ this .$emit (' remove-from-cart' , {product: this .product , variant: selectedVariant })
99
+ }
100
+
86
101
addReview(review : any ) {
87
102
const reviews = this .product .reviews as any ;
88
103
reviews .push (review .rating );
@@ -141,6 +156,18 @@ button.add-to-cart {
141
156
position: absolute;
142
157
top: 85px;
143
158
right: 13px;
159
+ }
160
+
161
+ button.remove-from-cart {
162
+ border: none;
163
+ background-color: #F12321;
164
+ color: white;
165
+ height: 40px;
166
+ width: 100px;
167
+ font-size: 14px;
168
+ position: absolute;
169
+ top: 135px;
170
+ right: 13px;
144
171
}
145
172
146
173
.disabledButton {
Original file line number Diff line number Diff line change 1
1
<template >
2
2
<div >
3
- <Product :premium =" premium" @add-to-cart =" updateCart" ></Product >
3
+ <Product :premium =" premium" @add-to-cart =" updateCart" @remove-from-cart = " removeFromCart " :cartLength = " cart.length " ></Product >
4
4
5
5
<div class =" cart" >
6
6
{{ cart.length }}
@@ -26,5 +26,10 @@ export default class Home extends Vue {
26
26
console .log (" Adding to cart:" , product );
27
27
this .cart .push (product );
28
28
}
29
+
30
+ removeFromCart(product : any ) {
31
+ let index = this .cart .indexOf (product );
32
+ this .cart .splice (index );
33
+ }
29
34
}
30
35
</script >
You can’t perform that action at this time.
0 commit comments