vuex实现的简单购物车功能示例
作者:东边的小山 发布时间:2024-05-08 10:43:19
标签:vuex,购物车
本文实例讲述了vuex实现的简单购物车功能。分享给大家供大家参考,具体如下:
购物车组件
<template>
<div>
<h1>vuex-shopCart</h1>
<div class="shop-listbox">
<shop-list/>
</div>
<h2>已选商品</h2>
<div class="shop-cartbox">
<shop-cart/>
</div>
</div>
</template>
<script>
import shopList from "./shop-list";
import shopCart from './shop-cart';
export default{
name:'shop',
components:{
'shop-list':shopList,
'shop-cart':shopCart
}
}
</script>
商品列表
<template>
<div class="shop-list">
<table>
<tr class="shop-listtitle">
<td>id</td>
<td>名称</td>
<td>价格</td>
<td>操作</td>
</tr>
<tr v-for="item in shopList" class="shop-listinfo">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.price}}</td>
<td><button @click="addToCart(item)">加入购物车</button></td>
</tr>
</table>
</div>
</template>
<script>
import{mapActions} from "vuex";
export default{
name:'shopList',
data(){
return{
}
},
computed:{
shopList(){
return this.$store.getters.getShopList
}
},
methods:{
...mapActions(['addToCart'])
}
}
</script>
<style lang="less" scoped>
@import url('../../static/public.less');
</style>
选中商品列表
<template>
<div class="shop-list">
<table>
<tr class="shop-listtitle">
<td>id</td>
<td>名称</td>
<td>价格</td>
<td>数量</td>
<td>操作</td>
</tr>
<tr v-for="item in cartData" class="shop-listinfo">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.price}}</td>
<td>{{item.num}}</td>
<td><button class="shop-dele dele-btn" @click="deletShop(item)">删除</button></td>
</tr>
<tr v-if="cartData.length<=0">
<td colspan="5">暂无数据</td>
</tr>
<tr>
<td colspan="2">总数:{{totalNum}}</td>
<td colspan="2">总价格:{{totalPrice}}</td>
<td><button class="dele-cart dele-btn" @click="clearCart">清空购物车</button></td>
</tr>
</table>
</div>
</template>
<script>
import {mapGetters,mapActions} from "vuex";
export default{
name:'shopCart',
data(){
return{
}
},
computed:{
...mapGetters({
cartData:'addShopList',
totalNum:'totalNum',
totalPrice:'totalPrice'
})
},
methods:{
...mapActions({
clearCart:'clearToCart',
deletShop:'deletToShop'
})
}
}
</script>
<style lang="less" scoped>
@import url('../../static/public.less');
.dele-btn{
background-color: red !important;
}
.dele-btn:hover{
background-color: #bd0000 !important;
}
</style>
vuex 创建
npm install vuex --save
,创建vuex文件夹,在文件夹中创建store.js,引入vuex;
import Vue from "vue";
import Vuex from 'vuex';
import cart from "./modules/cart";
Vue.use(Vuex);
export default new Vuex.Store({
modules:{
cart
}
})
建立一个模块文件夹modules,里面创建创建当个store模块,然后默认输出,在store.js中引入;
const state = {
shop_list: [{
id: 11,
name: '鱼香肉丝',
price: 12,
}, {
id: 22,
name: '宫保鸡丁',
price: 14
}, {
id: 34,
name: '土豆丝',
price: 10
}, {
id: 47,
name: '米饭',
price: 2
},{
id: 49,
name: '蚂蚁上树',
price: 13
},
{
id: 50,
name: '腊肉炒蒜薹',
price: 15
}],
add:[]
}
const getters ={
//获取商品列表
getShopList:state => state.shop_list,
//获取购物车列表
addShopList:state => {
return state.add.map(({id,num})=>{
let product = state.shop_list.find(n => n.id == id);
if(product){
return{
...product,
num
}
}
})
},
//获取总数量
totalNum:(state,getters) =>{
let total =0;
getters.addShopList.map(n=>{
total += n.num;
})
return total;
},
//计算总价格
totalPrice:(state,getters)=>{
let total=0;
getters.addShopList.map(n=>{
total += n.num*n.price
})
return total;
},
}
const actions={
//加入购物车
addToCart({commit},product){
commit('addCart',{
id:product.id
})
},
//清空购物车
clearToCart({commit}){
commit('clearCart')
},
//删除单个物品
deletToShop({commit},product){
commit('deletShop',product)
}
}
const mutations ={
//加入购物车
addCart(state,{id}){
let record = state.add.find(n => n.id == id);
if(!record){
state.add.push({
id,
num:1
})
}else{
record.num++;
}
},
//删除单个物品
deletShop(state,product){
state.add.forEach((item,i)=>{
if(item.id == product.id){
state.add.splice(i,1)
}
})
},
//清空购物车
clearCart(state){
state.add=[];
}
}
export default{
state,
getters,
actions,
mutations
}
希望本文所述对大家vue.js程序设计有所帮助。
来源:https://blog.csdn.net/yelin042/article/details/80216821


猜你喜欢
- 1、<DIV id=div1><h1>This is an DIV</h1></div> &
- 背景golang版本:1.16之前遇到的问题,docker启动时禁用了oom-kill(kill后服务受损太大),导致golang内存使用接
- HTML与CSS在Flash中的应用:不小心看到同事Den在弄个小东西:在Flash里使用HTML和CSS,代码是这样:var m
- Python tcp socket编程详解初学脚本语言Python,测试可用的tcp通讯程序:服务器:#!/usr/bin/env pyth
- 更多的信息,可以参考python内部的json文档: python>>> help(json) 或者官方文档: http:
- 一个简单的PHP循环一维数组的实例,先是把字符串按照一定的规则进行转换成为数组,然后再进行遍历输出,实际是一个很简单的方法,因为最近做的一个
- 这个是今年年初写的一篇,拿出来温习下。指针让程序结构变得混乱,也让程序执行效率提高,因此在oo的语言中不提倡指针的使用,使得程序结构清晰易读
- 在python中除了print函数之外,len函数和type函数应该算是使用最频繁的API了,操作都比较简单。一.len函数简介返回对象的长
- pycharm指定python路径,pycharm配置python环境的方法是:1、依次点击【File】、【Project Interpre
- 安装配置MongoDB驱动安装驱动go get -u github.com/mongodb/mongo-go-driver初始化模块go m
- 操作系统 : Windows 10_x64 [版本 10.0.19042.685]pjsip版本 : 2.10pjsip官网:https:/
- 这是群里一朋友问的问题,当时我说判断下 day 是否相邻即可,后来细想,发现完全不对。问题需求给定5个相同格式的日期,怎么判断是否是连续5天
- 前言:前面提到了Python中的数值型内置数据类型,接下来呢我们就着重介绍一下字符串类型。在Python中字符串是一个有序的字符集合,没有独
- 1. 概述在Numpy 1.24版本中,删除了像np.float、np.int 这样的 Python 内置类型的 alias,因此以后在代码
- 在flask更新到1.0之后的版本,官方推荐使用flask run的方式运行程序,可是作为开发,如果没有了pycharm的断点调试,这可太难
- 最近终于找到一个好的方法,使用Python的OpenCV模块识别滑动验证码的缺口,可以将滑动验证码中的缺口识别出来了。 测试使用如
- 在学习asyncio之前,先理清楚同步/异步的概念:同步是指完成事务的逻辑,先执行第一个事务,如果阻塞了,会一直等待,直到这个事务完成,再执
- 压缩复制删除文件基于python语言怎么操作呢,压缩文件有四种格式:zip、rar、tar、tar.gz,在压缩过程中也容易出现很多问题,今
- 本文实例讲述了Python实现字符串反转的常用方法。分享给大家供大家参考,具体如下:下面是实现python字符串反转的四种方法:1. 切片d
- 这个问题我在给新云CMS升级时遇到了,按照升级步骤做完,后台登录时,出现“HTTP 错误 500.100 - 内部服务器错误 - ASP 错