vue怎么实现可改变购物数量的购物车
短信预约 -IT技能 免费直播动态提醒
本篇内容主要讲解“vue怎么实现可改变购物数量的购物车”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“vue怎么实现可改变购物数量的购物车”吧!
效果图:
知识点:
1.computed 计算属性
2.filters 过滤器
实现代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<script class="lazy" data-src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
table {
border-collapse: collapse;
border-spacing: 0;
border: 1px solid #ccc;
}
td,
th {
padding: 8px 16px;
border: 1px solid #ccc;
text-align: left;
}
th {
background-color: #f7f7f7;
color: #5c6b77;
}
</style>
<body>
<div id="box">
<div v-if="books.length">
<table>
<thead>
<tr>
<th></th>
<th>书籍名字</th>
<th>出版日期</th>
<th>价格</th>
<th>购买数量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in books">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.date}}</td>
<td>{{item.price | toprice}}</td>
<td>
<button @click="down(index)" :disabled="item.aunt<=1">-</button> {{item.aunt}}
<button @click="add(index)">+</button>
</td>
<td>
<button @click="remove(index)">移除</button>
</td>
</tr>
</tbody>
</table>
<h2>总价:{{getallprice | toprice}}</h2>
</div>
<h2 v-else>您没有购物信息</h2>
</div>
<script>
const vm = new Vue({
el: "#box",
data: {
books: [{
id: 1,
name: "《vue.js实战》",
date: "2010.2.4",
price: 82.00,
aunt: 1
}, {
id: 2,
name: "《javascript实战》",
date: "2010.2.4",
price: 108.00,
aunt: 1
}, {
id: 3,
name: "《html+css实战》",
date: "2010.2.4",
price: 42.50,
aunt: 1
}, {
id: 4,
name: "《axios实战》",
date: "2010.2.4",
price: 82.00,
aunt: 1
}, {
id: 5,
name: "《jquery实战》",
date: "2010.2.4",
price: 65.20,
aunt: 1
}, ]
},
methods: {
add(index) {
this.books[index].aunt++;
},
down(index) {
this.books[index].aunt--;
},
remove(index) {
this.books.splice(index, 1)
},
},
computed: {
getallprice() {
let all = 0;
for (let i = 0; i < this.books.length; i++) {
all += this.books[i].price * this.books[i].aunt
}
return all
}
},
filters: {
toprice(price) {
return "¥" + price.toFixed(2)
},
}
})
</script>
</body>
</html>
到此,相信大家对“vue怎么实现可改变购物数量的购物车”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341