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
}
vuex——做简单的购物车功能的更多相关文章
- jQuery使用cookie与json简单实现购物车功能
本文实例讲述了jQuery使用cookie与json简单实现购物车的方法.分享给大家供大家参考,具体如下: 1.生成一个cookie 用来存储商品的id String类型 2.添加商品id的时候 把 ...
- JavaScript做简单的购物车效果(增、删、改、查、克隆)
比如有时候遇到下面这种情况,点击加入购物车,然后在上方的购物车中动态的添加商品以及商品的信息,我们就可以通过JavaScript实现简单的这些操作. 首先我们需要在html文档中,通过css对页面的布 ...
- 利用Vue实现一个简单的购物车功能
开始学习Vue的小白,dalao多多指正 想要实现下面这个界面,包含总价计算.数量增加和移除功能 话不多说代码如下 <!DOCTYPE html> <html> <hea ...
- vue实现简单的购物车功能
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- Python入门之实现简单的购物车功能
Talk is cheap,Let's do this! product_list = [ ['Iphone7 Plus', 6500], ['Iphone8 ', 8200], ['MacBook ...
- vue2.0实现购物车功能
购物车功能是一件比较繁琐的事情,逻辑功能太多,今天就用vue2.0实现一个简单的购物车功能,数据都本地自己写的假数据 功能列表: 1.全选和单选结算 2.减少和增加数量 3.商品的删除 界面搭建以及布 ...
- php 实现简单购物车功能(2)
上一篇的时候只是写了简单的加入购物车功能,购物车中产品的删除.提交订单后,库存的减少 以及客户账户的余额都没有完善, 这一篇是接着完善上一篇的,上一篇写到了购物车中删除的功能了,为了使删除的代码少敲一 ...
- react做的简单的购物车
###第一步 :首先电脑上已经安装react的脚手架 cnpm install create-react-app -g ###第二步 :创建项目 creact-react-app 项目 ...
- 用vuex实现购物车功能
效果图 展示目录结构 product组件(纯静态代码) cart组件(纯静态代码) info组件(纯静态代码) 完成以上的三个组件,现在要开始调用这些组件,在App.vue中调用 如果你的姿势正确的话 ...
随机推荐
- 关于“ubuntu18.04下网易云无法启动”的问题解决方案
1. 最简单的解决方案(参考文章:亢奋猫): 更正:在更改启动文件netease-cloud-music.desktop时应为:将第11行处的“Exec=netease-cloud-music %U” ...
- 函数语法:Js之on和addEventListener的使用与不同
一.addEventListener语法 DOM标准:elem.addEventListener("事件名",函数对象,是否在捕获阶段触发) ---是否在捕获阶段触发= true/ ...
- 第20月第9天 paddlepaddle
1. http://staging.paddlepaddle.org/docs/develop/book/02.recognize_digits/index.cn.html
- ubuntu14.04 Samba服务无法访问 网络名不再可用的问题
参考链接 : https://blog.csdn.net/liuyixjtu/article/details/54575514
- 使用Docker部署javaWeb应用
1. 安装Dcoker http://www.cnblogs.com/zhangqian27/p/9089815.html 2. 查看镜像 $ docker images 3. 搜索镜像 $ dock ...
- tomcat BIO / NIO
BIO NIO ref https://www.jianshu.com/p/76ff17bc6dea http://gearever.iteye.com/blog/1841586
- react-踩坑记录——Link带参数跳转后this.props为空对象
原因,自组件在挂载时,父组件没向其传props划线部分不可缺少!!!!!!
- centos系统下安装python3以及pip3
首先查看一下系统当前的python版本 python -V 1.安装必要工具 yum-utils 它的功能是管理repository及扩展包的工具yum install yum-utils -y 2. ...
- IntelliJ IDEA 配置
1.让IntelliJ IDEA 驼峰选择生效 驼峰选择,就是选择的时候按照驼峰规则选择单词,不是选择整个单词 让IntelliJ IDEA 驼峰选择生效 2.查看当前类中的所有方法 Alt+7 3. ...
- python三大神器
Python 中有很多优秀的包,本文主要讲一下 pip, virtualenv, fabric 1. pip 用来包管理 文档:https://pip.pypa.io/en/latest/instal ...