小米购物车

news/2024/11/8 14:37:31/

今天是国庆节,祝伟大的祖国生日快乐!!

假期适当的休息一下,还是要敲敲代码的!冲冲冲

今天我们写的是小米购物车!So easy!

老规矩,先看实现效果

数据直接存储到本地的,在后面我会把数据写上 : ) 

左上角的数据是实时响应的,当点击加入购物车时,数量会依次增加。

点击购物车会跳转到购物车页面,里面有两个我们刚刚加入的商品 ,价格也是直接算好。还有加减数量

当数量减到1个的时候就不能再减了,此时会弹出提示框

 

 1.在vuex中创建数据,将本地存储的数据存进去。

本地数据

[{"id":1,"name":"小米9 SE","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5652bd7a2f8bd6daaa1dd4b5c3c2e9b3.jpg","price":1999,"slogan":"新品看点:弹出式肩键|真实机械感反馈","totalMount":1,"totolPrice":0},{"id":2,"name":"Redmi K40 游戏增强版","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/61454401f855cf5ed64747a6ac04bae5.jpg","price":2999,"slogan":"新品看点:年度旗舰骁龙870,新一代 E4 AMOLED旗舰直屏","totalMount":1,"totolPrice":0},{"id":3,"name":"小米9 SE","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5652bd7a2f8bd6daaa1dd4b5c3c2e9b3.jpg","price":3999,"slogan":"新品看点:弹出式肩键|真实机械感反馈","totalMount":1,"totolPrice":0},{"id":4,"name":"Redmi K40 游戏增强版","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/61454401f855cf5ed64747a6ac04bae5.jpg","price":4999,"slogan":"新品看点:年度旗舰骁龙870,新一代 E4 AMOLED旗舰直屏","totalMount":1,"totolPrice":0},{"id":5,"name":"小米9 SE","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5652bd7a2f8bd6daaa1dd4b5c3c2e9b3.jpg","price":999,"slogan":"新品看点:弹出式肩键|真实机械感反馈","totalMount":1,"totolPrice":0},{"id":6,"name":"Redmi K40 游戏增强版","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/61454401f855cf5ed64747a6ac04bae5.jpg","price":2999,"slogan":"新品看点:年度旗舰骁龙870,新一代 E4 AMOLED旗舰直屏","totalMount":1,"totolPrice":0},{"id":7,"name":"小米9 SE","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5652bd7a2f8bd6daaa1dd4b5c3c2e9b3.jpg","price":1999,"slogan":"新品看点:弹出式肩键|真实机械感反馈","totalMount":1,"totolPrice":0},{"id":8,"name":"Redmi K40 游戏增强版","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/61454401f855cf5ed64747a6ac04bae5.jpg","price":699,"slogan":"新品看点:年度旗舰骁龙870,新一代 E4 AMOLED旗舰直屏","totalMount":1,"totolPrice":0},{"id":9,"name":"小米9 SE","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5652bd7a2f8bd6daaa1dd4b5c3c2e9b3.jpg","price":799,"slogan":"新品看点:弹出式肩键|真实机械感反馈","totalMount":1,"totolPrice":0},{"id":10,"name":"Redmi K40 游戏增强版","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/61454401f855cf5ed64747a6ac04bae5.jpg","price":1099,"slogan":"新品看点:年度旗舰骁龙870,新一代 E4 AMOLED旗舰直屏","totalMount":1,"totolPrice":0},{"id":11,"name":"小米9 SE","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5652bd7a2f8bd6daaa1dd4b5c3c2e9b3.jpg","price":1199,"slogan":"新品看点:弹出式肩键|真实机械感反馈","totalMount":1,"totolPrice":0},{"id":12,"name":"Redmi K40 游戏增强版","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/61454401f855cf5ed64747a6ac04bae5.jpg","price":1299,"slogan":"新品看点:年度旗舰骁龙870,新一代 E4 AMOLED旗舰直屏","totalMount":1,"totolPrice":0},{"id":13,"name":"小米9 SE","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5652bd7a2f8bd6daaa1dd4b5c3c2e9b3.jpg","price":1399,"slogan":"新品看点:弹出式肩键|真实机械感反馈","totalMount":1,"totolPrice":0},{"id":14,"name":"Redmi K40 游戏增强版","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/61454401f855cf5ed64747a6ac04bae5.jpg","price":1499,"slogan":"新品看点:年度旗舰骁龙870,新一代 E4 AMOLED旗舰直屏","totalMount":1,"totolPrice":0},{"id":15,"name":"小米9 SE","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5652bd7a2f8bd6daaa1dd4b5c3c2e9b3.jpg","price":15999,"slogan":"新品看点:弹出式肩键|真实机械感反馈","totalMount":1,"totolPrice":0},{"id":16,"name":"Redmi K40 游戏增强版","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/61454401f855cf5ed64747a6ac04bae5.jpg","price":17999,"slogan":"新品看点:年度旗舰骁龙870,新一代 E4 AMOLED旗舰直屏","totalMount":1,"totolPrice":0},{"id":18,"name":"小米9 SE","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5652bd7a2f8bd6daaa1dd4b5c3c2e9b3.jpg","price":18999,"slogan":"新品看点:弹出式肩键|真实机械感反馈","totalMount":1,"totolPrice":0},{"id":19,"name":"Redmi K40 游戏增强版","img":"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/61454401f855cf5ed64747a6ac04bae5.jpg","price":19999,"slogan":"新品看点:年度旗舰骁龙870,新一代 E4 AMOLED旗舰直屏","totalMount":1,"totolPrice":0}
]

 获取数据,在此之前要把数据手动存储到Application中

state: {getData:[]//在vuex中创建数据
},
mutations: {SET_DATA(state,data){const {getData}=datastate.getData=getData}
},
actions: {setData({commit}){//将本地存储的数据存到创建的数据中const getData=JSON.parse(localStorage.getItem('phoneData'))||[];commit('SET_DATA',{getData})}
}

此时我就拿到数据了,然后再页面循环数据

2.渲染数据

<template><div class="home"><header><span class="mititle">小米手机</span><router-link to="/about" class="gocar">购物车(0)</router-link></header><div class="fristpage"><ul><li v-for="item in getData" :key="item.id"><div class="shopimg"><img :src="item.img" alt=""></div><div class="shopcont"><h3 class="shopname">{{item.name}}</h3><p class="shopmore">{{item.slogan}}</p><p class="shopprice">¥{{item.price}}</p><button>加入购物车</button></div></li></ul></div></div>
</template>
.home{position: relative;float: left;width: 100%;height:6.67rem;border-radius: .04rem;overflow: hidden;header{height: 0.44rem;line-height: 0.44rem;background: #ff6704;color: white;padding: 0 0.2rem;display: flex;justify-content: center;align-content: center;span{font-size: 0.2rem;font-weight: 700;}.gocar{float: right;font-size: 0.1rem;color: white;position: absolute;right: 0.2rem;}}.fristpage{height: 6.23rem;overflow-x: scroll;ul{list-style: none;li{min-height: 1rem;border-bottom: solid 1px #999;position: relative;.shopimg{width: 25%;float: left;img{width: 100%;}}.shopcont{width: 72%;text-align: left;float: right;padding-top: 0.1rem 0 0 0.1rem;.shopmore{font-size: 0.1rem;height: 0.4rem;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;color: #999;}.shopprice{color: red;}button{background: white;border: 1px solid #ff6704;color: #ff6704;outline: none;position: absolute;right: 0.2rem;top: 0.7rem;}}}}
}
}

在js中调用vuex里面的数据,并在初始化加载数据

import {mapState} from 'vuex'
export default {name: 'Home',computed:{...mapState(['getData'])},created(){this.$store.dispatch('setData')}
}

3.创建购物车数据,将加入购物车的数据存放到里面

import Vue from 'vue'
import Vuex from 'vuex'Vue.use(Vuex)export default new Vuex.Store({state: {getData:[],//在vuex中创建数据carData:[]},getters: {},mutations: {SET_DATA(state,data){const {getData}=datastate.getData=getData},SET_CAR_DATA(state,data){const {carData}=datastate.carData=carData},SET_CAR(state,data){// 加入购物车时,要把对应的数据一同加进去const {id,img,name,price,slogan,_type}=data;const index=state.carData.findIndex(item=>item.id==id)if(index==-1){state.carData.push({id,img,name,price,slogan,_type,mount:1,tPrice:price,select:true})}else{let item=state.carData[index]//将添加,减少的 方法写在这里if(_type=='ADD'){item.mount+=1;item.tPrice+=price;}else if(_type=='DEL'){if(item.mount>1){item.mount-=1;item.tPrice-=price;}else{alert("数量不能减少了")}}}localStorage.setItem('carData',JSON.stringify(state.carData))//存储到本地中}},actions: {setData({commit}){//将本地存储的数据存到创建的数据中const getData=JSON.parse(localStorage.getItem('phoneData'))||[];commit('SET_DATA',{getData})},setCarData({commit}){const carData=JSON.parse(localStorage.getItem('carData'))||[];commit('SET_CAR_DATA',{carData})},setCar({commit},data){commit('SET_CAR',data)}},modules: {}
})

给加入购物车添加点击按钮,且调用vuex里面的方法

<button @click="addCar(item,'ADD')">加入购物车</button>

在js中调用方法,且传入对应的参数和数据

methods:{addCar(item,_type){this.$store.dispatch('setCar',{id:item.id,name:item.name,img:item.img,price:item.price,slogan:item.slogan,_type})}}

4.在购物车页面中渲染购物车的数据

<template><div class="about"><header><router-link to="/" class="back">返回</router-link><span class="cartitle">购物车</span></header><div class="carpage"><ul><li v-for="item in carData" :key="item.id"><div class="shopimg"><img :src="item.img" alt=""></div><div class="shopcont"><h3 class="shopname">{{item.name}}</h3><p class="shopmore">{{item.slogan}}</p><p class="shopprice">¥{{item.price}}</p><p class="adddel"><button class="del" @click="append(item,'DEL')">-</button><span class="num">{{item.mount}}</span><button class="add" @click="append(item,'ADD')">+</button></p></div></li></ul></div><footer><span>合计:¥0</span></footer></div>
</template>
<style lang="less" scoped>.about{position: relative;float: left;width: 100%;height:6.67rem;border-radius: .04rem;overflow: hidden;header{height: 0.44rem;line-height: 0.44rem;background: #ff6704;color: white;padding: 0 0.2rem;display: flex;justify-content: center;align-content: center;span{font-size: 0.2rem;font-weight: 700;}.back{float: right;font-size: 0.1rem;color: white;position: absolute;left: 0.2rem;}}.carpage{height: 5.79rem;overflow-y: scroll;ul{list-style: none;li{min-height: 1rem;border-bottom: solid 1px #999;position: relative;.shopimg{width: 25%;float: left;img{width: 100%;}}.shopcont{width: 72%;text-align: left;float: right;padding-top: 0.1rem 0 0 0.1rem;.shopmore{font-size: 0.1rem;height: 0.4rem;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;color: #999;}.shopprice{color: red;}.adddel{position: absolute;right: 0.1rem;top: 0.7rem;button{float: left;padding: 0.05rem;height: 0.2rem;width: 0.2rem;border: solid 1px #999;line-height: 0.1rem;text-align: center;border-radius: 50%;margin-right: 0.1rem;background: #fff;}.num{display: block;float: left;padding: 0.05rem;height: 0.1rem;width: 0.1rem;border: solid 1px #999;line-height: 0.1rem;text-align: center;margin-right: 0.1rem;font-size: 0.1rem;}}}}}}footer{height: 0.44rem;border-top: solid 1px #999;line-height: 0.44rem;text-align: right;padding: 0 0.2rem;}
}
</style>
<script>
import {mapActions} from 'vuex'
export default {name: 'Home',computed:{//调用数据carData(){return this.$store.state.carData}},methods:{...mapActions(['setCar','setCarData']),append(item,_type){if(item.select){if(_type=='ADD' && item.id==-1){this.setCar({tPrice:item.price,mount:1})}else if(_type=='DEL' && item.id==-1){if(item.mount>1){this.setCar({tPrice:-item.price,mount:-1})}}}this.setCar({id:item.id,price:item.price,_type})}},mounted(){this.setCarData()}
}
</script>

此时购物车的总数量和总价格没有实现,其他都ok

5.在vuex的getters中写入总数量和总价格实现方法

getters: {Mount(state){let mount=0state.carData.forEach((item)=>{mount+=item.mount})return mount},Price(state){let price=0state.carData.forEach((item)=>{price+=item.mount*item.price})return price}}

分别在computed引用相对应的方法

总数量

totalMount(){return this.$store.getters.Mount}

总价格

totalPrice(){return this.$store.getters.Price}

然后再HTML中直接渲染数据

<router-link to="/about" class="gocar">购物车({{totalMount}})</router-link>
<footer><span>合计:¥{{totalPrice}}</span>
</footer>

欧克,今天就到这里,加油!!

假期愉快


http://www.ppmy.cn/news/242822.html

相关文章

虚拟内存原理介绍

文章目录 1. 虚拟内存介绍2. 虚拟寻址3. 虚拟地址空间3. 页表4. 地址翻译5. TLB加速地址翻译6. 多级页表7. 页面置换算法 1. 虚拟内存介绍 我们知道系统中的所有进程都是共享CPU和主存资源&#xff0c;但这样就会存在一个问题&#xff0c;这么多进程怎么知道主存上的一块内存是…

【Matter】Nordic Mattter(开发大纲)

nRF Connect SDK 支持Mattter Nordic提供的Matter用户指南 子页面&#xff1a; Matter概况开始使用Matter如何创建 Matter 最终产品 Matter网络拓扑结构 Thread&#xff1a;Thread是一种开放的低功耗无线通信协议&#xff0c;旨在为物联网设备提供安全、稳定、高效的IPv6连接。…

Linux :: 【基础指令篇 :: 文件内容操作:(4)】:: head / tail 指令 :: 查看前后 n 行内容 | 指定查看文件的部分内容

前言&#xff1a;本篇是 Linux 基本操作篇章的内容&#xff01; 笔者使用的环境是基于腾讯云服务器&#xff1a;CentOS 7.6 64bit。 学习集&#xff1a; C 入门到入土&#xff01;&#xff01;&#xff01;学习合集Linux 从命令到网络再到内核&#xff01;学习合集 注&#xff…

Jmter压测试

1、常规性能测试--压测 1、添加线程组 线程数模拟用户数&#xff0c;线程数1表示1个用户&#xff0c;如果模拟10个用户就设置线程数为10 Ramp-Up表示在多长时间内开启多少个线程&#xff0c;如果设置为10&#xff0c;表示10s内开启对应的线程数 循环次数 永远表示如果不惦记…

Ubuntu加装4T机械硬盘

1、查看磁盘列表 fdisk -l #查看到目标磁盘: /dev/sdc 2、删除磁盘(/dev/sdc1&#xff0c;磁盘已经使用过&#xff0c;存在原分区) fdisk /dev/sdc #操作磁盘sdc Command (m for help): d …

Linux 挂载4T硬盘到根目录

参考&#xff1a;https://blog.csdn.net/qq_39450259/article/details/115518524 https://blog.csdn.net/jiabinzhang4310/article/details/100183731 1. 格式化4T硬盘 # fdisk -lDisk /dev/sdb: 4000.8 GB, 4000787030016 bytes, 7814037168 sectors Units sectors of 1 * …

android系统支持4T硬盘吗,电脑是否有可能,支持3T硬盘,但不支持4T硬盘

【问题描述】: USB外设异常、无法正常识别 【原因分析】&#xff1a; 静电,主板电压和系统设置等原因导致 【简易步骤】&#xff1a; 1. 拔出所有USB设备—关机—拔掉电源(笔记本需拔出电池)--按开机键5-6下—开机重新插入USB设备 2. 右键点击【我的电脑】—【设备管理】—【通…

linux格式化分区4t硬盘,Linux下挂载4T大小usb硬盘的经历

【注】都是从网络上学来的&#xff01;只是我记录了一下&#xff0c;免的将来又要挂盘的时候还要一通忙活&#xff01; (用ntfs-3g没有挂成功&#xff01;) 1、先去win7下进入磁盘管理里在这个大盘上单击右键&#xff0c;转换成GPT分区格式&#xff01; 2、挂到Linux机器上&…