vuex_cart案例

json-server使用
在目录下新建db文件夹=>里面新建index.json
index.json

{"cart": [{"id": 100001,"name": "低帮城市休闲户外鞋天然牛皮COOLMAX纤维","price": 128,"count": 6,"thumb": "https://yanxuan-item.nosdn.127.net/3a56a913e687dc2279473e325ea770a9.jpg"},{"id": 100002,"name": "网易味央黑猪猪肘330g*1袋","price": 39,"count": 15,"thumb": "https://yanxuan-item.nosdn.127.net/d0a56474a8443cf6abd5afc539aa2476.jpg"},{"id": 100003,"name": "KENROLL男女简洁多彩一片式室外拖","price": 128,"count": 3,"thumb": "https://yanxuan-item.nosdn.127.net/eb1556fcc59e2fd98d9b0bc201dd4409.jpg"},{"id": 100004,"name": "云音乐定制IN系列intar民谣木吉他","price": 589,"count": 1,"thumb": "https://yanxuan-item.nosdn.127.net/4d825431a3587edb63cb165166f8fc76.jpg"}],"friends": [{"id": 1,"name": "zs","age": 18},{"id": 2,"name": "ls","age": 19},{"id": 3,"name": "ww","age": 20}],"arr": [{"id": 1,"name": "zhansan"}]
}

在 cmd 终端定位到 db 文件夹目录下
json-server.cmd --watch index.json开启服务模拟服务端接口数据

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.js封装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注册并使用子组件,并渲染商品item

<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>

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>

cart-item.vue封装商品item

<template><div class="cart-item"><img :src="item.thumb" alt="" height="100px"><div class="wrapper"><p class="tit">{{ item.name }}</p><div class="box"><button @click="changeCount(-1)">-1</button><div class="count">{{ item.count }}</div><button @click="changeCount(1)">+1</button></div><div class="price">{{ item.price }}</div></div></div>
</template><script>
export default {props: {item: {type: Object}},methods: {changeCount(step) {console.log(this.item)const id = this.item.idconst newCount = this.item.count + stepthis.$store.dispatch('cart/updateCountAsync', { id, count: newCount })}}
}
</script><style scoped >
.cart-item {height: 140px;display: flex;align-items: center;
}.wrapper {margin-left: 10px;
}.box {display: flex;width: 80px;justify-content: space-between;align-items: center;
}.box button {width: 30px;height: 20px;border-radius: 3px;border: 0;background-color: orange;
}.tit,
.box {margin-bottom: 5px;
}
</style>

cart-footer.vue封装底部

<template><div><p>商品总数:{{ totalCount }} | 总价:{{ totalSum }}</p></div>
</template><script>
import { mapGetters } from 'vuex'
export default {computed: {...mapGetters('cart', ['totalCount', 'totalSum'])}
}
</script><style scoped></style>

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

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

相关文章

Nodejs压缩图片实现方案

安装sharp 目前选择使用sharp 0.31.2版本 npm i sharp0.31.2 并且在.npmrc里面配置镜像源 sharp_dist_base_urlhttps://npmmirror.com/mirrors/sharp-libvips/v8.13.3/ sharp_binary_hosthttps://npmmirror.com/mirrors/sharp sharp_libvips_binary_hosthttps://npmmirror.…

Java8新特性stream和parallelStream有什么区别

1 stream和parallelStream的区别 1.Stream 是在 Java8 新增的特性&#xff0c;普遍称其为流&#xff1b;它不是数据结构也不存放任何数据&#xff0c;其主要用于集合的逻辑处理。 2.Stream流是一个集合元素的函数模型&#xff0c;它并不是集合&#xff0c;也不是数据结构&…

Redis数据类型

目录 前言一、数据类型二、Redis单线程模型三、String类型四、什么是业务五、Hash类型六、List类型七、SET类型八、ZEST类型 前言 一、数据类型 Redis主要有Strings、Lists、Sets、Hashes、Sorted sets等数据类型&#xff0c;这些都是非常通用的&#xff0c;还有一些少见的可…

腾讯张乐:“反内卷”潮流已至,研发效能是软件企业必由之路

目录 Why&#xff5c;“狂飙”踩下刹车&#xff0c;“湖水岩石效应”加速显现 What&#xff5c;效能 ≠ 效率&#xff0c;效能 效率 有效性 How&#xff5c;研发效能“黄金三角” e.g.&#xff5c;软件研发效能实践中的“坑”与“解” 1. 忽视重视工程师的声音 2. “迷…

C# 按钮的AcceptButton和CancelButton属性

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System

使用iCloud和Shortcuts实现跨设备同步与自动化数据采集

在如今的数字时代&#xff0c;跨设备同步和自动化数据采集对于提高工作效率和便利性至关重要。苹果的iCloud和Shortcuts App为我们提供了强大的工具&#xff0c;可以实现跨设备同步和自动化数据采集的功能。本文将详细介绍如何利用iCloud和Shortcuts App实现这些功能&#xff0…

Vue3 使用 echarts

echarts 是百度基于 JavaScript 实现的一个开源可视化图表库&#xff0c;主要特点就是可视化类型丰富、动画炫酷、使用简单。 这个教程就简单演示如何在 Vue 3 项目中使用 echarts。 1、导入 echarts import * as echarts from "echarts";2、初始化 echarts 并设置…

day55 动规.p15 子序列

- 392.判断子序列 cpp class Solution { public: bool isSubsequence(string s, string t) { vector<vector<int>> dp(s.size() 1, vector<int>(t.size() 1, 0)); for (int i 1; i < s.size(); i) { for (int j 1; …

电力智能监控系统

电力智能监控系统依托电易云-智慧电力物联网&#xff0c;利用计算机、计量保护装置和总线技术&#xff0c;对中、低压配电系统的实时数据、开关状态及远程控制进行了集中管理。该电力监控系统可以为企业提供"监控一体化"的整体解决方案&#xff0c;主要包括实时历史数…

前端缓存方法有哪些?cookie有哪些属性?

这里写目录标题 前端缓存方法有哪些&#xff1a;cookie有哪些属性&#xff1f; 前端缓存方法有哪些&#xff1a; Browser Cache&#xff08;浏览器缓存&#xff09;: 当浏览器请求一个资源&#xff08;例如图片、CSS、JS 文件&#xff09;时&#xff0c;它会首先检查自己的缓存…

python中字典常用函数

字典常用函数 cmp(dict1,dict2) &#xff08;已删除&#xff0c;直接用>,<,即可&#xff09; 如果两个字典的元素相同返回0&#xff0c;如果字典dict1大于字典dict2返回1&#xff0c;如果字典dict1小于字典dict2返回-1。 先比较字典的长度&#xff0c;然后比较键&#x…

固定资产卡片乱怎么管理

固定资产卡片是记录公司固定资产信息的重要工具&#xff0c;如果管理不善&#xff0c;容易造成卡片混乱、数据错误等问题。 为了避免这种情况的发生&#xff0c;可以采取以下措施&#xff1a;  建立完善的资产管理制度&#xff0c;明确固定资产的分类、标准和使用情况&#x…

Hadoop -HDFS常用操作指令

1.启动HDFS hadoop/sbin/start-dfs.sh2.关闭 HDFS hadoop/sbin/stop-dfs.sh3. 在HDFS中创建文件夹 #老版本 hadoop fs -mkdir -p path #新版本 hadoop dfs -mkdir -p path4.查看指定目录下内容 hadoop fs -ls [-h] [-R] path hadoop dfs -ls [-h] [-R] ptahpath 指定…

2023开学礼《乡村振兴战略下传统村落文化旅游设计》许少辉八一新书山东师范大学图书馆

2023开学礼《乡村振兴战略下传统村落文化旅游设计》许少辉八一新书山东师范大学图书馆

客户忠诚度和保留率:不良方法的陷阱

良好的客户忠诚度和保留策略是任何电子商务业务成功的关键因素。但当出现问题时会发生什么&#xff1f;您可以采取哪些措施来鼓励忠诚度并减少客户流失&#xff1f;继续阅读以了解不良客户忠诚度和保留实践的后果。 忠诚度和保留率低下的后果 客户不满意和高流失率 客户忠诚…

如何在Mac电脑上安装WeasyPrint:简单易懂的步骤

1. 安装homebrew 首先需要确保安装了homebrew&#xff0c;通过homebrew安装weasyprint可以将需要的库都安装好&#xff0c;比pip安装更简单快捷。 安装方法如下&#xff1a; /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)&qu…

苹果微信聊天记录删除了怎么恢复?果粉原来是这样恢复的

粗心大意删除了微信聊天记录&#xff1f;有时候&#xff0c;一些小伙伴可能只是想要删除一部分聊天记录&#xff0c;但是在进行批量删除时&#xff0c;不小心勾选到了很重要的对话&#xff0c;从而导致记录丢失。 如果这时想找回聊天记录该怎么办&#xff1f;微信聊天记录删除…

算法笔记 二叉搜索树

二叉搜索树&#xff08;Binary Search Tree&#xff0c;简称 BST&#xff09;是一种数据结构&#xff0c;用于存储具有可比较键&#xff08;通常是数字或字符串&#xff09;的元素 1 结构特点 节点结构&#xff1a;每个节点都有一个键和两个子节点&#xff08;左子节点和右子…

服务器管理系统是什么

服务器管理系统是什么 服务器管理系统&#xff0c;是在操作系统下对操作系统的服务器软件及其相关软件进行二次设置的管理软件&#xff0c;是运营商管理域名、服务器、企业邮局、数据库等服务器主机类产品的一个网站平台&#xff0c;以达到快捷实现域名、服务器主机、企业邮局…

Wrist PPG数据集

Wrist PPG数据集通常是指采集自腕部&#xff08;手腕&#xff09;的光学心率传感器数据集&#xff0c;用于监测心率、心律变异性和其他生理信号。PPG代表光学脉搏波形&#xff0c;它是通过测量皮肤表面的光反射来获得心率信息的一种非侵入性方法。这些数据集通常用于研究心脏健…