一、第一种比较简单
效果图
实现代码:
购物车案例 *{ padding: 0; margin:0 } ul li{ width: 1200px; display: flex; align-items: center; justify-content: center; } li div,.total{ display: inline-block; width:200px; height: 50px; line-height: 50px; text-align: center; } button{ width: 60px; height: 40px; text-align: center; line-height: 40px; }
{handleCount(type,item)}" @ondelete="()=>{handleDelete(item.id)}"> 总价:{{total}}
二、一个用vue实现的简单响应式购物车案例
实现结果
如上,所有书类数据存在数组里,遍历显示在表格中,点击+和-可以实现数量和总价格的响应式变化,其中,减号到1时便添加了disabled类型,无法点击。 价格显示时通过过滤器显示的,加上Z¥符号和两位小数。项目结构为三个文件。
index.html
Title
| 书籍日期 | 出版日期 | 价格 | 购买数量 | 操作 | |
|---|---|---|---|---|---|
| {{item.id}} | {{item.name}} | {{item.date}} | {{item.price | showPrice}} | {{item.count}} |
购物车为空
main.js
const app = new Vue({
el:"#app",
data: {
books: [
{
id: 1,
name: '算法导论',
date: '2019-01-10',
price: 85.00,
count: 1
},
{
id: 2,
name: '计算机导论',
date: '2019-02-14',
price: 90.00,
count: 2
},
{
id: 3,
name: '科学导论',
date: '2019-09-10',
price: 85.21,
count: 1
},
{
id: 4,
name: '网络导论',
date: '2019-08-21',
price: 19.02,
count: 1
},
]
},
methods:{
getFinalPrice(price) {
return '$' + price.toFixed(2)
},
increment(index){
this.books[index].count--
},
decrement(index){
this.books[index].count++
},
removeHandler(index){
this.books.splice(index,1)
}
},
filters:{
showPrice(price){
return '$' + price.toFixed(2)
}
},
computed:{
totalprice(){
let tprice = 0
for(let i = 0; i< this.books.length; i++)
{
tprice += this.books[i].price * this.books[i].count
}
return tprice
}
}
})
style.css
table{
border: 1px solid #e9e9e9;
border-collapse: collapse;
bordre-spacing: 0;
}
th, td {
padding: 8px 16px;
border: 1px solid #e9e9e9;
text-align: left;
}
th{
backgroud-color: #f7f7f7;
color: #5c6b77;
font-weight: 600;
}
到此这篇关于Vue实现购物车实例代码的文章就介绍到这了,更多相关Vue 购物车内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!



