购物车

购物车具备两个功能:

  1. 保存用户需要的数据
  2. 清空

所以,我们使用vuex来实现购物车

1. 添加购物车 路由

向 文件 src/router/index.js 中增加内容:

  1. import Cart from '@/components/Cart'
  2. Vue.use(Router)
  3. export default new Router({
  4. routes: [
  5. { path: '/cart',
  6. name: 'Cart',
  7. component: Cart
  8. },
  9. ]
  10. })

2. 添加查看购物车的vue页面

新增 src/components/Car.vue 文件,内容如下:

  1. <template>
  2. <div class="background">
  3. <div id="my_cart">
  4. <CartHeaderView></CartHeaderView>
  5. <CartMainView></CartMainView>
  6. <NavBottomView :is_cart="is_cart"></NavBottomView>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. import CartHeaderView from './CartHeader.vue';
  12. import CartMainView from './CartMain.vue';
  13. import NavBottomView from './NavBottom.vue';
  14. export default{
  15. data () {
  16. return {
  17. is_cart: true
  18. }
  19. },
  20. mounted(){
  21. },
  22. components:{
  23. CartHeaderView,
  24. CartMainView,
  25. NavBottomView
  26. }
  27. }
  28. </script>

3. 增加对应的组件

3.1 新增购物车的头部文件 src/components/CartHeader.vue:

  1. <template>
  2. <div id="carttp">
  3. <header class="top_bar">
  4. <a onclick="window.history.go(-1)" class="icon_back"></a>
  5. <h3 class="cartname">购物车</h3>
  6. </header>
  7. </div>
  8. </template>

3.2 新增购物车的主体内容 src/components/CartMain.vue:

  1. <template>
  2. <main class="cart_box">
  3. <p v-show="!products.length"><i>请选择商品加入到购物车</i></p>
  4. <div class="cart_content clearfix" v-for="item in products" style="position: relative;">
  5. <div class="cart_shop clearfix">
  6. <div class="cart_check_box">
  7. <div class="check_box" checked>
  8. </div>
  9. </div>
  10. <div class="shop_info clearfix">
  11. <span class="shop_name" style="font-size: 14px;">丝路乐购新疆商城</span>
  12. </div>
  13. </div>
  14. <div @click="find_item_id(item)" class="cart_del clearfix" style="display: inline-block; position: absolute; top: 10px; right: 10px;">
  15. <div class="del_top">
  16. </div>
  17. <div class="del_bottom">
  18. </div>
  19. </div>
  20. <div class="cart_item">
  21. <div class="cart_item_box">
  22. <div class="check_box">
  23. </div>
  24. </div>
  25. <div class="cart_detial_box clearfix">
  26. <a class="cart_product_link">
  27. <img :src="item.image" alt="">
  28. </a>
  29. <div class="item_names">
  30. <a>
  31. <span>{{item.title}}</span>
  32. </a>
  33. </div>
  34. <div class="cart_weight">
  35. <span class="my_color" style="color: #979292;">{{item.title}}</span>
  36. </div>
  37. <div class="cart_product_sell">
  38. <span class="product_money"><strong class="real_money">{{item.price}}</strong></span>
  39. <div class="cart_add clearfix">
  40. <span @click="minus(item.id)" class="my_jian">-</span>
  41. <input type="tel" class="my_count" :value="item.quantity">
  42. <span @click="add(item.id)" class="my_add">+</span>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. <div class="pop" style="display: none">
  49. <div class="pop_box">
  50. <div class="del_info">
  51. 确定要删除该商品吗?
  52. </div>
  53. <div class="del_cancel">
  54. 取消
  55. </div>
  56. <div @click="deleteItem" class="del_ok">
  57. 确定
  58. </div>
  59. </div>
  60. </div>
  61. <div class="cart_fo">
  62. <footer class="cart_footer">
  63. <div class="count_money_box">
  64. <div class="heji">
  65. <strong>合计:</strong>
  66. <strong style="color: #ff621a; font-size: 18px;">\{\{ total | currency }}</strong>
  67. </div>
  68. <a :disabled="!products.length" @click="checkout(products)" class="go_pay">
  69. <span style="color: #f5f5f5; font-weight: 600;">结算</span>
  70. </a>
  71. </div>
  72. </footer>
  73. </div>
  74. </main>
  75. </template>
  76. <script>
  77. import { mapGetters } from 'vuex'
  78. import { go } from '../libs/router'
  79. import {check,animatDelBox} from '../assets/js/cart.js'
  80. export default{
  81. data(){
  82. return{
  83. need_delete_item: {},
  84. cartDatas:[ ],
  85. }
  86. },
  87. mounted(){
  88. check();
  89. animatDelBox();
  90. },
  91. computed: {
  92. ...mapGetters({
  93. products: 'cartProducts',
  94. checkoutStatus: 'checkoutStatus'
  95. }),
  96. total () {
  97. return this.products.reduce((total, item) => {
  98. return total + item.price * item.quantity
  99. }, 0)
  100. }
  101. },
  102. methods: {
  103. // 跳转到支付页面
  104. checkout (products) {
  105. go("/shops/dingdanzhifu", this.$router)
  106. },
  107. // 对于商品的数量进行增加
  108. add (id) {
  109. this.$store.dispatch('changeItemNumber', {id, type: 'add'})
  110. },
  111. // 对于商品的数量进行减少
  112. minus (id) {
  113. this.$store.dispatch('changeItemNumber', {id, type: 'minus'})
  114. },
  115. // 删除某个商品
  116. deleteItem () {
  117. this.$store.dispatch('deleteItem', this.need_delete_item.id)
  118. },
  119. find_item_id (item) {
  120. this.need_delete_item = item
  121. }
  122. },
  123. }
  124. </script>

3.3 修改Vuex的函数

把下面代码添加到 src/vuex/actions.js 文件中:

  1. export const deleteItem = ({ commit }, id) => {
  2. commit(types.DELETE_ITEM, {
  3. id: parseInt(id)
  4. })
  5. }
  6. export const changeItemNumber = ({ commit }, {id, type}) => {
  7. console.info(id)
  8. commit(types.CHANGE_ONE_QUANTITY, {
  9. id: parseInt(id),
  10. type
  11. })
  12. }

上面的代码, 实现了购物车的若干功能:

  • 对于商品数量的增减
  • 实现了当商品数量改变时,商品总价也跟着修改。

看效果

购物车做好后,点击打开,如下图所示:

购物车页面

总结

  • 购物车的数据, 是通过Vuex来保存的
  • 购物车中数量的加减,是需要直接影响到商品总价的。 这个角度来看,用Vuex来做更加适合。