vue实现书籍购物车功能
更新时间:2021年10月27日 10:58:55 作者:今天会下雨吗
这篇文章主要为大家详细介绍了vue实现书籍购物车功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了vue实现书籍购物车功能的具体代码,供大家参考,具体内容如下
效果图
点击增加、减少购买数量和移除总价格会变化

代码
<!DOCTYPE html>
<html lang="en">
<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>书籍购物车</title>
<style>
table{
border: 1px solid #e9e9e9;
border-collapse: collapse;
border-spacing: 0;
}
th, td{
padding: 8px 16px;
border: 1px solid #e9e9e9;
text-align: left;
}
th{
background-color: #f7f7f7;
color: #5c6b77;
font-weight: 600;
}
</style>
</head>
<body>
<div id="app" v-cloak>
<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>{{index+1}}</td>
<td>{{item.name}}</td>
<td>{{item.date}}</td>
<td>{{item.price | showPrice}}</td>
<td>
<!-- disabled 为true时按钮禁用 -->
<button @click="reduce(index)" v-bind:disabled="item.count <= 1">-</button>
{{item.count}}
<button @click="increase(index)">+</button>
</td>
<td><button @click="remove(index)">移除</button></td>
</tr>
</tbody>
</table>
<h2>总价格:{{totalPrice | showPrice}}</h2>
</div>
<h2 v-else>购物车为空</h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: "#app",
data:{
books:[
{
name: '《算法导论》',
date: '2021-8-1',
price: 85.00,
count: 1
},
{
name: '《UNIX编程艺术》',
date: '2021-8-2',
price: 69.00,
count: 1
},
{
name: '《编程珠玑》',
date: '2021-8-3',
price: 35.00,
count: 1
},
{
name: '《DOM编程艺术》',
date: '2021-8-4',
price: 75.00,
count: 1
},
{
name: '《nodejs深入浅出》',
date: '2021-8-5',
price: 105.00,
count: 1
},
],
},
methods:{
reduce(index){
this.books[index].count--;
},
increase(index){
this.books[index].count++;
},
remove(index){
this.books.splice(index,1);
},
},
computed:{
// 写在计算属性里的方法可以直接当属性使用
totalPrice(){
//let totalPrice = 0;
// 1. 普通的for循环
// for (let i = 0; i < this.books.length; i++) {
// totalPrice += this.books[i].count * this.books[i].price;
// }
// 2. 步骤稍简单的普通for循环
// for (let i in this.books) {
// totalPrice += this.books[i].count * this.books[i].price;
// }
// 3. for(let item of this.books)
//for(let item of this.books){
//totalPrice += item.count * item.price;
//}
//return totalPrice;
// 4. 高阶函数写法 reduce
// 直接返回结果 不需要定义变量,也不需要遍历
return this.books.reduce(function(pre, book){
return pre + book.price * book.count;
},0);
},
// 过滤器
filters:{
showPrice(price){
return "¥" + price.toFixed(2);
}
}
})
</script>
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
vee-validate vue 2.0自定义表单验证的实例
今天小编就为大家分享一篇vee-validate vue 2.0自定义表单验证的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-08-08
vxe-table 实现 excel 选择一个单元格拖拽自动复制新的单元格(示例代码)
vxe-table是一款强大的表格组件,支持Excel风格的操作,通过鼠标右下角的扩展按钮,用户可以拖拽选择单元格并自动复制内容到扩展区域的所有单元格中,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧2025-01-01
vue 监听input输入事件(oninput)的示例代码支持模糊查询
这篇文章主要介绍了vue 监听input输入事件(oninput)支持模糊查询,比如说表格模糊查询,实现一边输入,一边过滤数据,本文通过示例代码给大家详细讲解,需要的朋友可以参考下2023-02-02


最新评论