浏览代码

[ADD] total calculate

Gogs 7 年之前
父节点
当前提交
b6126c7aa6
共有 2 个文件被更改,包括 19 次插入4 次删除
  1. 12 2
      src/components/CartTotal.vue
  2. 7 2
      src/store/modules/cart.js

+ 12 - 2
src/components/CartTotal.vue

@@ -2,10 +2,20 @@
     .cart-total
         h2
             small.cart-total-currency Gs
-            | 
-            | 0
+            |
+            | {{ total }}
 </template>
 
+<script>
+    import { mapGetters } from 'vuex'
+
+    export default {
+        computed: mapGetters({
+            total: 'getTotal'
+        })
+    }
+
+</script>
 
 <style lang="sass">
     .cart-total

+ 7 - 2
src/store/modules/cart.js

@@ -7,6 +7,9 @@ const getters = {
     getCartItems(state) {
         return state.cart
     },
+    getTotal(state) {
+        return state.total
+    },
     isEmpty(state) {
         return state.cart.length !== 0
     }
@@ -26,9 +29,11 @@ const mutations = {
         let sum = 0
 
         state.cart.forEach(item => {
-            sum = sum + (item.list_price * item.qty)
+            sum = sum + (item.list_price * (item.qty || 1))
         })
-        
+
+        console.log(sum)
+
         state.total = sum
     }
 }