基于vuex实现购物车功能


Posted in Vue.js onJanuary 10, 2021

本文实例为大家分享了vuex实现购物车功能的具体代码,供大家参考,具体内容如下

先看效果:

基于vuex实现购物车功能

代码:

<template>
 <div class="Home">
 <h1>vuex购物车案例</h1>
 <add-from></add-from>
 <shop-cart></shop-cart>
 </div>
</template>

<script>
import AddFrom from './Add.vue'
import ShopCart from './ShopCart.vue'
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'
export default {
 name: 'Home',
 components: {
 AddFrom,
 ShopCart
 },

};
</script>
<style>
table {
 width: 800px;
 margin: 0 auto;
 border: 1px solid #ccc;
 border-spacing: 0;
}
tbody th,
tbody td {
 border: 1px solid #ccc;
 text-align: center;
}
h1{
 text-align: center;
}
.add{
 width: 800px;
 margin: 0 auto;
}
</style>

父组件

<template>
 <div class="add">
 <div class="from-group">
 <label for="">商品编号</label>
 <input type="text" v-model="shop.id" placeholder="请输入商品编号">
 </div>
 <div class="from-group">
 <label for="">商品名称</label>
 <input type="text" v-model="shop.name" placeholder="请输入商品名称">
 </div>
 <div class="from-group">
 <label for="">商品价格</label>
 <input type="text" v-model="shop.price" placeholder="请输入商品价格">
 </div>
 <div class="from-group">
 <label for="">商品数量</label>
 <input type="text" v-model="shop.count" placeholder="请输入商品数量">
 </div>
 <div class="from-group">
 <button @click="add">添加商品</button>
 </div>
 </div>
</template>

<script>
export default {
 name: 'add',
 data() {
 return {
 shop:{}
 };
 },
 methods:{
 add(){
 
 this.$store.dispatch("addShopList",this.shop)
 this.shop = {
 id:"",
 name:"",
 price:"",
 count:""
 }
 }
 }
};
</script>

<style scoped>
 .add{
 width: 800px;
 text-align: center;
 }
</style>

```bash

<template>
 <div class="Shop-Cart">

 <table>
 <thead border>
 <tr>
  <th>商品编号</th>
  <th>商品名称</th>
  <th>商品价格</th>
  <th>商品数量</th>
  <th>小计</th>
  <th>操作</th>
 </tr>
 </thead>
 <tbody v-if="shop.length > 0">
 <tr v-for="(i, e) in shop" :key="e">
  <td>{{ i.id }}</td>
  <td>{{ i.name }}</td>
  <td>{{ i.price }}</td>
  <td>
  <button @click="add(e)">+</button>
  <input type="text" v-model="i.count" />
  <button @click="delet(e)">-</button>
  </td>
  <td>¥{{ i.price * i.count }}</td>
  <td><button @click="del(e)">删除</button></td>
 </tr>
 </tbody>
 <tfoot>
 <tr>
  <td colspan="6" align="right">总计:{{ total }}</td>
  <button @click="clear">清除购物车</button>
 </tr>
 </tfoot>
 </table>
 </div>
</template>

<script>
export default {
 name: 'Shop-Cart',
 components: {},
 data() {
 return {};
 },
 computed: {
 shop() {
 return this.$store.getters.getlist;
 },
 total() {
 return this.$store.getters.getShopTotal;
 }
 },
 methods: {
 del(e) {
 // this.$store.dispatch()
 this.$store.dispatch('remoteList', e);
 },

 add(e) {
 this.$store.dispatch('addList', e);
 },
 delet(e) {
 this.$store.dispatch('deleteList', e);
 },

 clear() {
 this.$store.dispatch('clearList', []);
 }
 }
};
</script>

vuex组件

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


export default new Vuex.Store({
 state: {
 list: [{
 id: 1,
 name: "哇哈哈",
 price: 3,
 count: 0
 },
 {
 id: 2,
 name: "哇哈",
 price: 3,
 count: 0
 }
 ]
 },
 getters: {
 //获取购物车数据
 getlist(state) {
 return state.list
 },
 //商品的总价
 getShopTotal(state,index) {
 let result = 0;
 state.list.forEach((item, index) => {
 result += item.price * item.count
 })
 return result
 },
 },
 mutations: {
 //删除购物车单个数据
 remoteList(state,index) {
 state.list.splice(index, 1);
 console.log("aaa",state)
 },
 //商品数量增加
 addList(state, index) {
 state.list[index].count++;
 },
 //商品数量减少
 deleteList(state, index) {
 state.list[index].count--;
 if(state.list[index].count<=0){
 state.list[index].count = 0
 return ;
 }
 },

 //清除购物车
 clearList(state, arr) {
 state.list = arr
 },
 addShopList(state,shop){
 state.list.push(shop)
 }
 },
 //使用actions调用mutations方法
 actions: {
 remoteList({
 commit
 }, index) {
 commit("remoteList", index)
 },
 addList({
 commit
 }, index) {
 commit("addList", index)
 },
 deleteList({
 commit
 }, index) {
 commit("deleteList", index)
 },
 clearList({
 commit
 }, arr) {
 commit("clearList", arr)
 },
 addShopList({commit},shop){
 commit("addShopList",shop)
 }
 },
 modules: {}
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Vue.js 相关文章推荐
详解vue实现坐标拾取器功能示例
Nov 18 Vue.js
如何正确解决VuePress本地访问出现资源报错404的问题
Dec 03 Vue.js
基于vue+echarts数据可视化大屏展示的实现
Dec 25 Vue.js
vue中配置scss全局变量的步骤
Dec 28 Vue.js
vue实现防抖的实例代码
Jan 11 Vue.js
vue实现一个获取按键展示快捷键效果的Input组件
Jan 13 Vue.js
Vue实现动态查询规则生成组件
May 27 Vue.js
Vue + iView实现Excel上传功能的完整代码
Jun 22 Vue.js
vite+vue3.0+ts+element-plus快速搭建项目的实现
Jun 24 Vue.js
Vue3中的Refs和Ref详情
Nov 11 Vue.js
vue项目proxyTable配置和部署服务器
Apr 14 Vue.js
vue router 动态路由清除方式
May 25 Vue.js
antdesign-vue结合sortablejs实现两个table相互拖拽排序功能
Jan 08 #Vue.js
vue-quill-editor插入图片路径太长问题解决方法
Jan 08 #Vue.js
vue编写简单的购物车功能
Jan 08 #Vue.js
解决vue使用vant轮播组件swipe + flex时文字抖动问题
Jan 07 #Vue.js
vuex的使用和简易实现
Jan 07 #Vue.js
vue watch监控对象的简单方法示例
Jan 07 #Vue.js
vue.js watch经常失效的场景与解决方案
Jan 07 #Vue.js
You might like
解析Linux下Varnish缓存的配置优化
2013/06/20 PHP
php 文件下载 出现下载文件内容乱码损坏的解决方法(推荐)
2016/11/16 PHP
ThinkPHP 整合Bootstrap Ajax分页样式
2016/12/23 PHP
如何打开php的gd2库
2017/02/09 PHP
Convert Seconds To Hours
2007/06/16 Javascript
面向对象的Javascript之二(接口实现介绍)
2012/01/27 Javascript
Javascript对象中关于setTimeout和setInterval的this介绍
2012/07/21 Javascript
JS声明变量背后的编译原理剖析
2012/12/28 Javascript
javaScript函数中执行C#代码中的函数方法总结
2013/08/07 Javascript
js限制文本框只能输入整数或者带小数点的数字
2015/04/27 Javascript
学习JavaScript设计模式之享元模式
2016/01/18 Javascript
深入浅析JavaScript中with语句的理解
2016/05/12 Javascript
Vue开发过程中遇到的疑惑知识点总结
2017/01/20 Javascript
详解从零搭建 vue2 vue-router2 webpack3 工程
2017/11/22 Javascript
jquery实现直播弹幕效果
2019/11/28 jQuery
解决三元运算符 报错“SyntaxError: can''t assign to conditional expression”
2020/02/12 Javascript
小程序开发之模态框组件封装
2020/04/23 Javascript
[04:09]2014DOTA2国际邀请赛Ti西雅图 历届冠军相继出局 BBC综述今日比赛
2014/07/20 DOTA
[05:08]第一届“网鱼杯”DOTA2比赛精彩集锦
2014/09/05 DOTA
[04:29]2016国际邀请赛中国区预选赛Ehome战队教练采访
2016/06/27 DOTA
python多线程http下载实现示例
2013/12/30 Python
django数据库migrate失败的解决方法解析
2018/02/08 Python
解决Matplotlib图表不能在Pycharm中显示的问题
2018/05/24 Python
Python从使用线程到使用async/await的深入讲解
2018/09/16 Python
Python3.5内置模块之os模块、sys模块、shutil模块用法实例分析
2019/04/27 Python
使用Python OpenCV为CNN增加图像样本的实现
2019/06/10 Python
CSS3实现的文本3D效果附图
2014/09/03 HTML / CSS
中秋节礼品促销方案
2014/02/02 职场文书
大学生第一学年自我鉴定
2014/09/12 职场文书
2014离婚协议书范文两篇
2014/09/15 职场文书
五四青年节活动总结
2015/02/10 职场文书
成品仓库管理员岗位职责
2015/04/09 职场文书
2015年采购员工作总结
2015/04/27 职场文书
党员干部廉洁自律承诺书
2015/04/28 职场文书
2016年优秀少先队辅导员事迹材料
2016/02/26 职场文书
Springboot配置suffix指定mvc视图的后缀方法
2021/07/03 Java/Android