vue.js学习(day 20)

综合案例:购物车 

数据渲染 

构建cart购物车模块 

 准备后端接口服务环境

 请求数据存入vuex

 

cart.js 
// 新建购物车模块
import axios from 'axios'
export default {namespaced: true,state () {return {// 购物车数据 [{},{}]list: []}},mutations: {updateList (state, newList) {state.list = newList}},actions: {// 请求方式:get// 请求地址:http://localhost:3000/cartasync getList (context) {const res = await axios.get('http://localhost:3000/cart')context.commit('updateList', res.data)}},getters: {}
}
App.vue 
<template><div class="app-container"><!-- Header 区域 --><cart-header></cart-header><!-- 商品 Item 项组件 --><cart-item v-for="item in list" :key="item.id" :item="item"></cart-item><!-- Foote 区域 --><cart-footer></cart-footer></div>
</template><script>
import CartHeader from '@/components/cart-header.vue'
import CartFooter from '@/components/cart-footer.vue'
import CartItem from '@/components/cart-item.vue'import { mapState } from 'vuex'
export default {name: 'App',created () {this.$store.dispatch('cart/getList')},computed: {...mapState('cart', ['list'])},components: {CartHeader,CartFooter,CartItem}
}
</script><style lang="less" scoped>
.app-container {padding: 50px 0;font-size: 14px;
}
</style>
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">-</button><span class="count">{{ item.count }}</span><button class="btn btn-light">+</button></div></div></div></div>
</template>
<script>
export default {name: 'CartItem',methods: {},props: {item: {type: Object,require: true}}
}
</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>

修改数量功能的实现 

cart.js 

// 新建购物车模块
import axios from 'axios'
export default {namespaced: true,state () {return {// 购物车数据 [{},{}]list: []}},mutations: {updateList (state, newList) {state.list = newList},// obj: {id:xxx, newCount:xxx}updateCount (state, obj) {// 根据 id 找到对应的对象,更新count属性即可const goods = state.list.find(item => item.id === obj.id)goods.count = obj.newCount}},actions: {// 请求方式:get// 请求地址:http://localhost:3000/cartasync getList (context) {const res = await axios.get('http://localhost:3000/cart')context.commit('updateList', res.data)},// 请求方式: patch// 请求地址: http://localhost:3000/cart/:id值 表示修改的是哪个对象// 请求参数:// {//    name:'新值',【可选】//    price:'新值',【可选】//    count:'新值',【可选】//    thumb:'新值',【可选】//    }async updateCountAsync (context, obj) {// 将修改更新同步到后台服务器await axios.patch(`http://localhost:3000/cart/${obj.id}`, {count: obj.newCount})// 将修改更新同步到 vuexcontext.commit('updateCount', {id: obj.id,newCount: obj.newCount})}},getters: {}
}

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',methods: {btnClick (step) {const newCount = this.item.count + stepconst id = this.item.idif (newCount < 1) returnthis.$store.dispatch('cart/updateCountAsync', {id,newCount})}},props: {item: {type: Object,require: true}}
}
</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>

 底部 getters 统计

cart.js 

// 新建购物车模块
import axios from 'axios'
export default {namespaced: true,state () {return {// 购物车数据 [{},{}]list: []}},mutations: {updateList (state, newList) {state.list = newList},// obj: {id:xxx, newCount:xxx}updateCount (state, obj) {// 根据 id 找到对应的对象,更新count属性即可const goods = state.list.find(item => item.id === obj.id)goods.count = obj.newCount}},actions: {// 请求方式:get// 请求地址:http://localhost:3000/cartasync getList (context) {const res = await axios.get('http://localhost:3000/cart')context.commit('updateList', res.data)},// 请求方式: patch// 请求地址: http://localhost:3000/cart/:id值 表示修改的是哪个对象// 请求参数:// {//    name:'新值',【可选】//    price:'新值',【可选】//    count:'新值',【可选】//    thumb:'新值',【可选】//    }async updateCountAsync (context, obj) {// 将修改更新同步到后台服务器await axios.patch(`http://localhost:3000/cart/${obj.id}`, {count: obj.newCount})// 将修改更新同步到 vuexcontext.commit('updateCount', {id: obj.id,newCount: obj.newCount})}},getters: {// 商品总数 sum + item.counttotal (state) {return state.list.reduce((sum, item) => sum + item.count, 0)},// 商品总结个 sum + item.count * item.pricetotalPrice (state) {return state.list.reduce((sum, item) => sum + item.count * item.price, 0)}}
}

 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://www.mzph.cn/news/888835.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

RAG系统分类、评估方法与未来方向

分享一篇RAG综述&#xff1a;Retrieval-Augmented Generation for Large Language Models: A Survey&#xff0c;主要想了解一下RAG的评估方法&#xff0c;分享给大家。 文章目录 一、RAG分类二、评估方法三、未来方向 一、RAG分类 RAG分类&#xff1a;Navie RAG、Advanced RA…

Ant Design Vue v4版本如何解决1px没有被postcss-px2rem转成rem的问题

背景说明 如果你的 Ant Design Vue 项目有要做适配的需求&#xff0c;那首先要选择一种适配方案。笔者选择的是用 postcss-px2rem 进行适配。笔者在配置了 postcss-px2rem的相关配置后&#xff0c;发现 postcss-px2rem 没有对 Ant Design Vue 进行适配。在网上看了一些文章之后…

美国大选后,用HMM模型做特斯拉股价波动解析

作者&#xff1a;老余捞鱼 原创不易&#xff0c;转载请标明出处及原作者。 写在前面的话&#xff1a;本文主要探讨如何利用高斯隐马尔可夫模型&#xff08;HMM&#xff09;预测股票价格&#xff0c;我们将分步进行说明&#xff1a;包括数据准备、特征选择、训练 HMM 模型、最后…

解决Conda虚拟环境中pip下载包总是到base环境的问题

conda本地创建的虚拟环境使用pip安装一些包总是安装到base环境中&#xff0c;导致无法正确进行环境隔离&#xff0c;下面是一些解决办法 方法一、使用python -m pip安装 1.1、验证虚拟环境的pip版本是哪个版本&#xff0c;如下所示&#xff0c;本人的demo虚拟环境直接使用pip…

VSCode(四)CMake调试

1. 工具准备 1.1 C环境插件 1.2 CMake插件 2. Cmake工程 2.1 创建项目文件夹 ex:CMAKE_TEST 2.2 创建CMake工程 &#xff08;shift ctl P), 选择"CMAKE: Quick Start": 2.3 填写project name: (ex: test_cmake) 2.4 选择”Executable“ 项目文件内会自动…

qt-everywher交叉编译e-src-5.15.2

简化配置的方式&#xff1a; 你完全可以通过直接配置 安装目录、编译链 和 目标架构 来完成交叉编译&#xff0c;而不需要修改 mkspecs 配置。以下是如何通过简化配置来进行交叉编译 Qt 的步骤。 准备交叉编译工具链 首先&#xff0c;确保你已经安装了交叉编译工具链&#xff…

kafka-clients之ConsumerConfig

Kafka ConsumerConfig 中的配置项用于定义消费者的行为&#xff0c;如消费方式、偏移管理、组协调等。以下是ConsumerConfig中的关键配置项及其详细说明&#xff1a; 1. bootstrap.servers 类型&#xff1a;List<String>说明&#xff1a;Kafka集群的地址列表&#xff0…

EasyExcel导出列表

通过easyexcel导出列表数据 根据列表内容自适应宽高。 文件名冲突&#xff0c;修改文件名递增设置。 依赖 <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>${easyexcel.version}</version&…

ubuntu下的chattts 学习4:Advanced Usage

源码 import ChatTTS import torch import torchaudiochat ChatTTS.Chat() chat.load(compileFalse) # Set to True for better performance ################################### # Sample a speaker from Gaussian.rand_spk chat.sample_random_speaker() print(rand_spk)…

从 HTML 到 CSS:开启网页样式之旅(七)—— CSS浮动

从 HTML 到 CSS&#xff1a;开启网页样式之旅&#xff08;七&#xff09;—— CSS浮动 前言一、浮动的简介1.没有浮动的代码和效果2.加入浮动的代码和效果 二、元素浮动后的特点1. 脱离文档流2.宽高特性&#xff1a;3.共用一行&#xff1a;4.margin 特性&#xff1a;5.区别于行…

微信小程序实现图片拖拽调换位置效果 -- 开箱即用

在编写类似发布朋友圈功能的功能时&#xff0c;需要实现图片的拖拽排序&#xff0c;删除图片等功能。 一、效果展示 **博主的小程序首页也采用了该示例代码&#xff0c;可以在威信中搜索&#xff1a;我的百宝工具箱 二、示例代码 1.1、在自己的小程序中创建组件 1.2、组件…

通过 FRP 实现 P2P 通信:控制端与被控制端配置指南

本文介绍了如何通过 FRP 实现 P2P 通信。FRP&#xff08;Fast Reverse Proxy&#xff09;是一款高效的内网穿透工具&#xff0c;能够帮助用户突破 NAT 和防火墙的限制&#xff0c;将内网服务暴露到公网。通过 P2P 通信方式&#xff0c;FRP 提供了更加高效、低延迟的网络传输方式…

MySQL Explain 指南

MySQL Explain 指南 idselect_typetablepartitionstypepossible_keyskeykeylenrefrowsfilteredExtra 使用 explain 执行 DML 语句时&#xff0c;数据不会发生变化。explain 的结果可能包含多行数据&#xff0c;每行对应一个表。若涉及 union 操作&#xff0c;MySQL 会创建临时表…

如何给 JavaScript 函数添加参数校验?

在 JavaScript 中&#xff0c;对函数参数进行校验是确保代码健壮性和防止错误的重要手段。参数校验不仅能提高代码的可读性&#xff0c;还能帮助捕获潜在的错误。下面&#xff0c;我们将结合实际项目代码示例&#xff0c;讲解如何给 JavaScript 函数添加参数校验。 常见的参数…

php7.4安装pg扩展-contos7

今天接到一个需求&#xff0c;就是需要用thinkphp6链接pg(postgresql)数据库。废话不多说&#xff0c;直接上操作步骤 一、安装依赖 yum install -y sqlite-devel libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-dev…

CentOS7.X 安装RustDesk自建服务器实现远程桌面控制

参照文章CentOS安装RustDesk自建服务器中间总有几个位置出错&#xff0c;经实践做个记录防止遗忘 一 环境&工具准备 1.1 阿里云轻量服务器、Centos7系统、目前最高1.1.11版本rustdesk-server-linux-amd64.zip 1.2 阿里云轻量服务器–安全组–开放端口&#xff1a;TCP(21…

TCP Analysis Flags 之 TCP Spurious Retransmission

前言 默认情况下&#xff0c;Wireshark 的 TCP 解析器会跟踪每个 TCP 会话的状态&#xff0c;并在检测到问题或潜在问题时提供额外的信息。在第一次打开捕获文件时&#xff0c;会对每个 TCP 数据包进行一次分析&#xff0c;数据包按照它们在数据包列表中出现的顺序进行处理。可…

c# 设计模式--抽象工厂模式 (Abstract Factory)

定义 抽象工厂模式是一种创建型设计模式&#xff0c;它提供了一种创建一系列相关或相互依赖对象的接口&#xff0c;而无需指定它们具体的类。抽象工厂模式强调的是对象族的创建&#xff0c;而不是单一对象的创建。 用例写法 假设我们有一个场景&#xff0c;需要根据不同的平…

MySQL 8.0 的主主复制(双向复制)

在 Windows Server 2022 Datacenter 上配置 MySQL 8.0 的主主复制&#xff08;双向复制&#xff09;&#xff0c;步骤与 Linux 类似&#xff0c;但有一些特定的配置和路径需要注意。以下是详细的简化步骤&#xff1a; 1. 使用 root 用户登录 确保你以 root 用户登录到 MySQL …

1. 设计模式的由来

设计模式的灵感来自建筑师亚历山大的“设计套路”&#xff0c;后来被程序员借用&#xff0c;总结出一套“编程武功秘籍”。 20世纪90年代&#xff0c;四位软件工程师&#xff08;被称为“四人帮”&#xff09;——Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides&…