基于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 相关文章推荐
vuex Module将 store 分割成模块的操作
Dec 07 Vue.js
在vue中动态修改css其中一个属性值操作
Dec 07 Vue.js
vue使用element-ui实现表单验证
Dec 13 Vue.js
vue3.0中使用element的完整步骤
Mar 04 Vue.js
解决Vue+SpringBoot+Shiro跨域问题
Jun 09 Vue.js
详细聊聊vue中组件的props属性
Nov 02 Vue.js
Vue+Flask实现图片传输功能
Apr 01 Vue.js
Vue+TypeScript中处理computed方式
Apr 02 Vue.js
vue中div禁止点击事件的实现
Apr 02 Vue.js
VUE之图片Base64编码使用ElementUI组件上传
Apr 09 Vue.js
解决vue中provide inject的响应式监听
Apr 19 Vue.js
Vue 打包后相对路径的引用问题
Jun 05 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
PHP程序级守护进程的实现与优化的使用概述
2013/05/02 PHP
ThinkPHP模板中数组循环实例
2014/10/30 PHP
php-fpm中max_children的配置
2019/03/15 PHP
PHP Swoole异步读取、写入文件操作示例
2019/10/24 PHP
JS宝典学习笔记(下)
2007/01/10 Javascript
IE和firefox浏览器的event事件兼容性汇总
2009/12/06 Javascript
window.onbeforeunload方法在IE下无法正常工作的解决办法
2010/01/23 Javascript
基于jQuery实现的当离开页面时出现提示的实现代码
2011/06/27 Javascript
jquery如何获取复选框的值
2013/12/12 Javascript
javascript中SetInterval与setTimeout的定时器用法
2015/08/24 Javascript
JavaScript中的Object对象学习教程
2016/05/20 Javascript
JavaScript数组去重由慢到快由繁到简(优化篇)
2016/08/26 Javascript
javascript中闭包概念与用法深入理解
2016/12/15 Javascript
JS实现的RGB网页颜色在线取色器完整实例
2016/12/21 Javascript
vue和react等项目中更简单的实现展开收起更多等效果示例
2018/02/22 Javascript
jquery获取select选中值的文本,并赋值给另一个输入框的方法
2018/08/21 jQuery
解决Vue开发中对话框被遮罩层挡住的问题
2018/11/26 Javascript
解决Layui数据表格显示无数据提示的问题
2019/11/14 Javascript
vue excel上传预览和table内容下载到excel文件中
2019/12/10 Javascript
Vue实现图片轮播组件思路及实例解析
2020/05/11 Javascript
python 全局变量的import机制介绍
2017/09/07 Python
Python中is和==的区别详解
2018/11/15 Python
python远程连接MySQL数据库
2019/04/19 Python
Python更新所有已安装包的操作
2020/02/13 Python
python用TensorFlow做图像识别的实现
2020/04/21 Python
Python+pyftpdlib实现局域网文件互传
2020/08/24 Python
python 实现非极大值抑制算法(Non-maximum suppression, NMS)
2020/10/15 Python
运行python提示no module named sklearn的解决方法
2020/11/29 Python
使用django自带的user做外键的方法
2020/11/30 Python
CSS3 box-sizing属性
2009/04/17 HTML / CSS
越南母婴用品购物网站:Kids Plaza
2020/04/09 全球购物
俄罗斯商务邀请函
2014/01/26 职场文书
2015年圣诞节活动总结
2015/03/24 职场文书
2015年禁毒工作总结
2015/04/30 职场文书
2015年学校办公室工作总结
2015/05/26 职场文书
推广普通话宣传标语口号
2015/12/26 职场文书