用Vue框架怎么写一个简易的购物车
Admin 2022-07-11 群英技术资讯 1717 次浏览
这篇文章主要介绍了用Vue框架怎么写一个简易的购物车相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇用Vue框架怎么写一个简易的购物车文章都会有所收获,下面我们一起来看看吧。本文实例为大家分享了Vue.js框架实现购物车的具体代码,供大家参考,具体内容如下
<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../lib/vue.min.js"></script>
</head>
<body>
<div id="app" style="position: relative;left: 30%">
<table cellpadding="10">
<thead>
<th><input type="checkbox" v-model="cb" v-on:click="allSelect">全选</th>
<th>名称</th>
<th>单价</th>
<th>数量</th>
<th>金额</th>
<th>操作</th>
</thead>
<tbody>
<tr v-for="x in info">
<td><input type="checkbox" v-model="x.bol" v-on:click="sign()"></td>
<td>{{x.name}}</td>
<td>{{x.price}}</td>
<td><input type="number" v-model="x.num" min="0" style="width: 50px;text-align: center" v-on:click="count(x)" v-on:change="count(x)"></td>
<td>{{x.total}}</td>
<td><button v-on:click="del(x)">删除</button></td>
</tr>
</tbody>
</table>
<Br>
<p>总金额:{{all}}</p>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
all: 0,
cb: false,
info: [{
bol: false,
name: "iphone7",
price: 6000,
num: 1,
total: 6000
}, {
bol: false,
name: "荣耀6x",
price: 1200,
num: 1,
total: 1200
}, {
bol: false,
name: "dell笔记本",
price: 4000,
num: 1,
total: 4000
}]
},
methods: {
//计算单价
count: function(obj) {
for(var i = 0; i < this.info.length; i++) {
//
if(this.info[i] == obj) {
this.info[i].total = obj.price * obj.num;
}
//如果被选中了,就计算总价格
if(obj.bol) {
this.allSelect();
}
}
},
//删除
del: function(obj) {
this.info.splice(this.info.indexOf(obj), 1)
this.allCount();
},
//单选
sign: function() {
this.allCount();
},
//全选
allSelect: function() {
for(var i = 0; i < this.info.length; i++) {
this.info[i].bol = this.cb;
}
this.allCount();
},
//计算总价
allCount: function() {
//每次计算总价都要清空
this.all = 0;
for(var i = 0; i < this.info.length; i++) {
//计算被选中的商品
if(this.info[i].bol) {
this.all += this.info[i].total;
}
}
}
}
})
</script>
</body>
</html>
效果图:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
文章中涉及 ref 的应用仅为父组件调用子组件场景下的应用方式,并未涵盖 ref 的所有应用方式!
如果调用者在函数返回时能够获得预期结果(即获得预期返回值或看到预期效果),则该函数同步。如果函数同步,即使调用函数执行的任务耗时,也会等到得到预期的结果。
jquery获取select选中的值示例详解,js获取选中option的value值很简单,下面看本jquery教程的:selected选择器的使用。 html代码部分 selectoptionvalue=1张三/optionoptionvalue=2selected=selected李四/optionoptionvalue=3王五/option/selectbuttonid=getS
配置文件:RedisOptions.jsconstoptions={host:'208.167.233.104',port:15001,password:'123456',detect_buffers:true//传入buffer返回也是buffer否则会转换成String}module.exports=options 封装r
Vue插槽,是学习vue中必不可少的一节,越来越发现插槽的好用,而过滤数据也是我们日常开发中必然会用到的,这篇文章主要给大家介绍了关于Vue插槽和过滤器的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008