File tree Expand file tree Collapse file tree 3 files changed +37
-2
lines changed
Expand file tree Collapse file tree 3 files changed +37
-2
lines changed Original file line number Diff line number Diff line change 2828 Add to Cart
2929 </button >
3030
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+
3139 <div v-for =" (variant, index) in product.variants"
3240 :key =" variant.id"
3341 class =" color-box"
@@ -52,6 +60,7 @@ import ProductReview from './ProductReview.vue';
5260})
5361export default class Product extends Vue {
5462 @Prop ({default: false }) private premium! : boolean ;
63+ @Prop () private cartLength: number ;
5564
5665 product = {
5766 name: " Vue Socks" ,
@@ -87,6 +96,12 @@ export default class Product extends Vue {
8796 this .$emit (' add-to-cart' , {product: this .product , variant: this .selectedVariant });
8897 }
8998
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+
90105 addReview(review : any ) {
91106 const reviews = this .product .reviews as any ;
92107 reviews .push (review .rating );
@@ -145,6 +160,18 @@ button.add-to-cart {
145160 position : absolute ;
146161 top : 85px ;
147162 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 ;
148175}
149176
150177.disabledButton {
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ export default new Vuex.Store({
3636// const productList = [{
3737// name: "Vue Socks",
3838// brand: "Vue",
39+ // price: 5,
3940// variants: [
4041// {id: 1, color: "green"},
4142// {id: 2, color: "blue"}
@@ -45,7 +46,8 @@ export default new Vuex.Store({
4546// },
4647// {
4748// name: "Angular Socks",
48- // brand: "Angular",
49+ // brand: "angular",
50+ // price: 15,
4951// variants: [
5052// {id: 1, color: "red"},
5153// {id: 2, color: "blue"}
@@ -56,6 +58,7 @@ export default new Vuex.Store({
5658// {
5759// name: "npm Socks",
5860// brand: "npm",
61+ // price: 3,
5962// variants: [
6063// {id: 1, color: "red"},
6164// ],
Original file line number Diff line number Diff line change 11<template >
22 <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 >
44
55 <div class =" cart" >
66 {{ cart.length }}
@@ -26,5 +26,10 @@ export default class Home extends Vue {
2626 console .log (" Adding to cart:" , product );
2727 this .cart .push (product );
2828 }
29+
30+ removeFromCart(product : any ) {
31+ let index = this .cart .indexOf (product );
32+ this .cart .splice (index );
33+ }
2934}
3035 </script >
You can’t perform that action at this time.
0 commit comments