Skip to content

Commit a881bfc

Browse files
committed
feat: add total calculation and item addition to shopping cart in example1
1 parent 043d897 commit a881bfc

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

‎editor.js‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ function example1() {
158158
</ul>
159159
<!-- v-else to show a fallback message if the list is empty -->
160160
<div v-if="cart.length === 0">No items in this shopping cart.</div>
161+
<div v-else>total: {{total}}</div>
161162
</div>
162163
163164
<br> <a href="product-1.html" >product-1</a> <br> <a href="product-2.html" >product-2</a> <br>
@@ -169,22 +170,29 @@ function example1() {
169170
<script>
170171
function init() {
171172
172-
if( ! window.parent.persistentData.cart )
173-
window.parent.persistentData.cart = []
173+
if( ! parent.persistentData.cart )
174+
parent.persistentData.cart = [];
174175
175-
vue = Vue.createApp({
176+
const vue = Vue.createApp({
176177
data() {
177-
return window.parent.persistentData
178+
return parent.persistentData
178179
},
179-
methods: {
180-
deleteItem(index) {
181-
this.cart.splice(index, 1); // Removes the task at the given index
180+
methods: {
181+
deleteItem(index) {
182+
this.cart.splice(index, 1); // Removes the task at the given index
183+
},
184+
addItem(item){
185+
this.cart.push(item);
186+
}
182187
},
183-
addItem(item){
184-
this.cart.push(item);
185-
}
186-
}
187-
}).mount('#app')
188+
computed: {
189+
total() {
190+
const cart = this.cart;
191+
if (!cart) return 0;
192+
return cart.reduce((total, item) => total + item.price, 0);
193+
}
194+
},
195+
}).mount('#app');
188196
189197
}
190198
</script>` // example1

0 commit comments

Comments
 (0)