Vue.js实现购物车功能

news/2025/2/28 4:34:33/

Vue.js实现购物车功能

1.功能实现
以表格的形式将存放在后端的购物信息展示在前端页面,实现步骤如下:
使用axios技术异步读取json文件里的数据,结合Vue.js的v-for指令将信息逐条迭代存放在表格中,在表格中展示“商品名称”、“商品单价”、“商品数量”等信息,并能对商品进行数量的增加、减少,清除此项订单信息的功能,订单总价随着商品数量的增加和减少实时更新。

2.order.json数据如下

      [{id:1,name:'华为Mate40',price:7299,count:2},{id:2,name:'iphone XR',price:6199,count:1,},{id:3,name:'小米10',price:3999,count:3}]

3.前端界面如下

<body><div id ="app" ><template v-if="list.length"><table border="1px" style="width: 400px;"><thead><tr><th>编号</th><th>商品名称</th><th>商品单价</th><th>购买数量</th><th>操作</th></tr></thead><tr v-for="(product,index) in list"><td>{{index+1}}</td><td>{{product.name}}</td><td>{{product.price}} </td><td><button @click="subProduct(index)" :disabled="product.count==1">-</button>{{product.count}}<button @click="addProduct(index)">+</button></td><td><button @click="removePriduct(index)">移除</button></td></tr><tbody></tbody></table><div>总金额:¥{{totalPrice}}</div></template><template v-else>购物车为空 </template>
</div>
</body>

4.js代码如下

 <script>//创建Vue实例,得到 ViewModelvar app = new Vue({el: '#app',
/*数据*/data: {list:[],
},
/*自动加载函数*/mounted(){//异步读取json文件数据axios.get('/json/order.json',{}).then(response(){app.list=response.data;);
},
/*执行触发函数*/methods: {//数量减少subProduct:function(index){this.list[index].count--;},//数量增加addProduct:function(index){this.list[index].count++;},//清除订单removePriduct:function(index){this.list.splice(index,1);}},
/*动态计算属性*/computed: {//返回总金额totalPrice:function(){var total=0;for (var i=0;i < this.list.length;i++){total+=this.list[i].price*this.list[i].count;}return total;}
},});</script>

效果图如下:
在这里插入图片描述


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

相关文章

oracle表碎片查询整理

模拟插入数据 create table test as select * from dba_objects; insert into test select * from test; insert into test select * from test; insert into test select * from test; commit;收集统计信息 SET TIME ON exec dbms_stats.gather_table_stats(ownname>TEST…

惠普战66五代锐龙版配置 怎么样

惠普战66五代锐龙版轻薄本A/C/D三面采用的是金属机身一体成型工艺&#xff0c;重约1.74kg &#xff0c;厚度19.9mm&#xff1b;屏幕均采用四面窄边框设计&#xff0c;屏占比87.9%&#xff0c;支持180 开合、100%SRGB高色域、400尼特亮度。尤为值得一提的是&#xff0c;惠普战66…

postfix邮件服务配置及各种问题解决

前言&#xff1a;本文总结了一些postfix常见的问题及解决方案&#xff0c;可能并不太全面&#xff0c;望对阅读的你有一些帮助 一、postfix邮件服务安装 1.安装 yum -y install postfix 2.配置 vim /etc/postfix/main.cf myhostname sample.abc.com  设置系统的主机名…

列表中套字典,根据字典的某个key删列表的元素

#根据name删除信息 stu_list [{name:杨杨,age:12,QQ:6199},{name:喜羊羊,age:16,QQ:2471},{name:懒洋洋,age:18,QQ:3569} 方法一&#xff1a;遍历列表&#xff0c;把字典中包含要删除的值不拼接到新列表 def appName(stu_list):new_list []del_name input(请输入你要删除信…

邮件服务器启动postfix时的问题:启动 postfix: [失败]

[rootlinux115 spool]# service postfix start 启动 postfix&#xff1a; [失败] [rootlinux115 log]# postfix start postsuper: fatal: scan_dir_push: open directory defer: Permission denied postfix/postfix-script: fatal: Postfix integrity check failed! [rootlinu…

微软最强 Python 自动化工具开源了!不用写一行代码!

1. 前言 最近&#xff0c;微软开源了一款非常强大的 Python 自动化依赖库&#xff1a;playwright-python 它支持主流的浏览器&#xff0c;包含&#xff1a;Chrome、Firefox、Safari、Microsoft Edge 等&#xff0c;同时支持以无头模式、有头模式运行 playwright-python 提供了同…

codeforces 1669F

题意: alice和bob从数组两边的吃糖果, 数组的值就是糖果重量 要求alice和bob吃的糖果重量必须一样, 输出能吃几个糖果 这题最先想到的是前后缀相加 模拟一个前缀和 和 后缀和 在n/2的位置向前找前缀和 在n/2的位置向后找后缀和 找到第一个前缀和后缀和的下标输出就好 …

LINUX6279

linux_填空题大全&#xff0d;金锄头文库 一、 选择题(每小题2分&#xff0c;共50分) 在创建Linux分区时&#xff0c;一般要创建(D )两个分区 FAT/NTFS B. FAT/SWAP C. NTFS/SWAP D.SWAP/根 分区 当登录Linux时&#xff0c;一个具有唯一进程ID号的shell将…