File tree Expand file tree Collapse file tree 3 files changed +34
-2
lines changed
Expand file tree Collapse file tree 3 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ <h1>{{ product }}</h1>
3535
3636 <!-- Events -->
3737 < button
38- v-on:click ="cart++; inventory-- "
38+ v-on:click ="addToCart "
3939 :disabled ="!inventory "
4040 class ="add-to-cart "
4141 :class ="inventory ? '' : 'disabledButton' "
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" ,
@@ -83,6 +92,12 @@ export default class Product extends Vue {
8392 this .$emit (' add-to-cart' , {product: this .product , variant: selectedVariant });
8493 }
8594
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+
86101 addReview(review : any ) {
87102 const reviews = this .product .reviews as any ;
88103 reviews .push (review .rating );
@@ -141,6 +156,18 @@ button.add-to-cart {
141156 position : absolute ;
142157 top : 85px ;
143158 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 ;
144171}
145172
146173.disabledButton {
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