File tree 3 files changed +37
-2
lines changed
3 files changed +37
-2
lines changed 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" ,
@@ -87,6 +96,12 @@ export default class Product extends Vue {
87
96
this .$emit (' add-to-cart' , {product: this .product , variant: this .selectedVariant });
88
97
}
89
98
99
+ removeFromCart() {
100
+ this .product .inventory ++ ;
101
+ const selectedVariant = this .product .variants [this .selectedVariantIndex ];
102
+ this .$emit (' remove-from-cart' , {product: this .product , variant: selectedVariant })
103
+ }
104
+
90
105
addReview(review : any ) {
91
106
const reviews = this .product .reviews as any ;
92
107
reviews .push (review .rating );
@@ -145,6 +160,18 @@ button.add-to-cart {
145
160
position: absolute;
146
161
top: 85px;
147
162
right: 13px;
163
+ }
164
+
165
+ button.remove-from-cart {
166
+ border: none;
167
+ background-color: #F12321;
168
+ color: white;
169
+ height: 40px;
170
+ width: 100px;
171
+ font-size: 14px;
172
+ position: absolute;
173
+ top: 135px;
174
+ right: 13px;
148
175
}
149
176
150
177
.disabledButton {
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ export default new Vuex.Store({
36
36
// const productList = [{
37
37
// name: "Vue Socks",
38
38
// brand: "Vue",
39
+ // price: 5,
39
40
// variants: [
40
41
// {id: 1, color: "green"},
41
42
// {id: 2, color: "blue"}
@@ -45,7 +46,8 @@ export default new Vuex.Store({
45
46
// },
46
47
// {
47
48
// name: "Angular Socks",
48
- // brand: "Angular",
49
+ // brand: "angular",
50
+ // price: 15,
49
51
// variants: [
50
52
// {id: 1, color: "red"},
51
53
// {id: 2, color: "blue"}
@@ -56,6 +58,7 @@ export default new Vuex.Store({
56
58
// {
57
59
// name: "npm Socks",
58
60
// brand: "npm",
61
+ // price: 3,
59
62
// variants: [
60
63
// {id: 1, color: "red"},
61
64
// ],
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