Vue+ElementUI+Vuex购物车

最完整最能理解的Vuex版本的购物车

购物车是最经典的小案例。

Vuex代码:

import Vue from 'vue'
import Vuex from 'vuex'
import $http from '../request/http'
Vue.use(Vuex)

const store =  new Vuex.Store({
    state:{
        shopList:[],

    },
    mutations:{
        setShopCarList(state,payload){
            state.shopList = payload
        },
        change_Num(state,payload){
            console.log(payload);
            state.shopList = payload
        }
    },
    actions:{
        //获取商品
      async getShopList(context){
           const result =   await $http.shopHttp.getShopcartData()
            context.commit('setShopCarList',result.data.data)
        },
        //改变数量
       async changeNumAsync(context,payload){
           
          const result = await  $http.shopHttp.changeNum(payload)
          console.log('result',result);
            if(result.code){
                context.dispatch('getShopList')
            }
        },
    },
    getters:{
        total(state){
            return state.shopList.filter(item=>item.checked).reduce((prev,cur)=>prev+cur.price*cur.num,0)
        }
    }
})
export default store

组件代码:

*<template>
  <div>
    <el-card>
        <el-table :data=" getshopCartList"  border>
          <el-table-column >
            <template slot-scope="scoped">
              <el-checkbox v-model="scoped.row.checked"></el-checkbox>
            </template>
          </el-table-column>
            <el-table-column prop="name" label="商品名称" align="center"></el-table-column>
            <el-table-column   label="数量" align="center">
              <template slot-scope="scope">
                <el-input-number   :min="1" v-model="scope.row.num" size="mini" @change="(currentValue,oldValue)=>changeShoppingNum(currentValue,oldValue,scope.row)" 
                ></el-input-number>
              </template>
            </el-table-column>
            <el-table-column prop="price" label="价格" align="center"></el-table-column>
            <el-table-column label="小计" align="center" >
              <template slot-scope="scope">
                {{ (scope.row.num * scope.row.price).toFixed(2)}}
              </template>op0-/
            </el-table-column>
            <el-table-column  label="更新时间" align="center">{{ getshopCartList.updateDate | dateFormat }}</el-table-column>
            
            <el-table-column  label="创建时间" align="center">{{ getshopCartList.createDate | dateFormat }}</el-table-column>
            
          </el-table>
          总价:{{ this.$store.getters.total }}
    </el-card>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data(){
    return {
        shopList:[],
        num:1,
        shopId:null
    }
  },
  methods:{
    changeShoppingNum(currentValue,oldValue,value){
      let num = currentValue - oldValue
      this.shopId = value._id
      if (!this.shopId) {
        return;
      }else {
        console.log(111);
        this.$store.dispatch('changeNumAsync',{_id:this.shopId,n:num})
      }
        
    },
  },
  computed:{
    getshopCartList:{
      get(){
        return this.$store.state.shopList
      }
    }
  },
  filters:{
      dateFormat(val,myFormat,count){
        return moment(val).format(myFormat || 'YYYY-MM-DD')+(count ? '--'+count :'')
      }
  },
  created(){
    this.$store.dispatch('getShopList')
  },
  watch:{
    getshopCartList:{
      deep:true,
      handler(newVal,oldVal) {
        console.log(1,newVal, 2,oldVal);
      }
    }
  }
}
</script>

购物车

1、elementUi 中计数器的使用,

<el-input-number setp=‘1’ :min=‘1’ v-model=‘scope.row.num’ size=‘mini’ @change=‘changeShoppingNum(scope.row)’><el-input-number>

注意:

1. el-input-number标签是有默认两个参数:currentValue newValue

2. 但是在需要默认参数的情况下,还需要自定义的参数可以使用回调参数的方法。

<el-input-number

@change="(currentValue,oldValue)=>changeShoppingNum(currentValue,oldValue,scope.row)">

</el-input-number>

2、Vuex的持续化:

使用计算属性。

3、计算数字去掉浮点数:

.toFixed(2)

4、elementui表格的selection放在el-table中,选择时返回的是所选择的整个对象
5、v-model与v-bind的区别:
  1. v-model多在表单中使用,在表单元素上创建双向绑定

  1. v-model属于语法糖,写法与使用v-bind给输入框绑定value属性值,并添加input事件实现的效果是一样的,

  1. v-bind用来绑定数据和属性以及表达式,缩写为':'

如果不与input事件,无法实现v-model的双向绑定动能。

  1. v-model是以Vue实例中的数据作为数据来源。所以应该在data中声明初始值来引用

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你提供一些实现购物车模块的思路。首先,购物车模块需要记录用户加入购物车的商品信息,可以使用Vue中的响应式数据来实现。其次,可以使用Element UI中的表格组件来展示购物车中的商品信息,同时也可以添加删除商品等操作。下面是一个简单的实现步骤: 1. 创建一个Vue组件来表示购物车模块,可以命名为Cart.vue。 2. 在组件中使用data属性来定义响应式数据,可以定义一个数组来存储购物车中的商品信息,例如: ```javascript data() { return { cartItems: [] } } ``` 3. 在组件的模板中使用Element UI中的表格组件来展示购物车中的商品信息,例如: ```html <el-table :data="cartItems"> <el-table-column prop="name" label="商品名称"></el-table-column> <el-table-column prop="price" label="商品价格"></el-table-column> <el-table-column prop="quantity" label="商品数量"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button @click="removeItem(scope.$index)">删除</el-button> </template> </el-table-column> </el-table> ``` 4. 在组件中定义添加商品和删除商品的方法,例如: ```javascript methods: { addItem(item) { const index = this.cartItems.findIndex(cartItem => cartItem.id === item.id) if (index >= 0) { this.cartItems[index].quantity++ } else { this.cartItems.push({ id: item.id, name: item.name, price: item.price, quantity: 1 }) } }, removeItem(index) { this.cartItems.splice(index, 1) } } ``` 5. 在使用购物车模块的页面中,可以引入Cart组件并传递商品信息给它,例如: ```html <template> <div> <h1>商品列表</h1> <ul> <li v-for="item in items" :key="item.id"> {{ item.name }} - {{ item.price }} <el-button @click="addToCart(item)">加入购物车</el-button> </li> </ul> <h1>购物车</h1> <cart :cart-items="cartItems"></cart> </div> </template> <script> import Cart from '@/components/Cart.vue' export default { components: { Cart }, data() { return { items: [ { id: 1, name: '商品1', price: 100 }, { id: 2, name: '商品2', price: 200 }, { id: 3, name: '商品3', price: 300 } ], cartItems: [] } }, methods: { addToCart(item) { this.$refs.cart.addItem(item) } } } </script> ``` 这样就可以实现一个简单的购物车模块了。当用户点击“加入购物车”按钮时,会调用addToCart方法将商品信息添加到购物车中;购物车组件会响应数据的变化,自动更新表格中的内容。用户也可以在购物车中删除商品,或者修改商品数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值