<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<script src="js/vue.global.js"></script>
</head>
<body>
<div id="app">
<table>
<capting>
<h1>购物车</h1>
</capting>
<tr>
<th></th>
<th>编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品数量</th>
<th>操作</th>
</tr>
<tr v-for='(cart,index) in cartList' >
<td><input type="checkbox" v-model='cart.check' /></td>
<td>{{cart.id}}</td>
<td>{{cart.name}}</td>
<td>{{cart.price}}</td>
<td>
<button @click='cart.count==0?0:cart.count--'>-</button>
{{cart.count}}
<button @click='cart.count++'>+</button>
</td>
<td><a href="#" @click='del(index)'>删除</a></td>
</tr>
<tr>
<td>总价</td>
<td>{{totalprice}}</td>
</tr>
</table>
</div>
<script>
const app=Vue.createApp({
data(){
return{
cartList:[{
id:1,
name:'《计算机》',
price:10,
count:1,
check:true
},
{
id:2,
name:'《计算机》',
price:10,
count:1,
check:true
},
{
id:3,
name:'《计算机》',
price:10,
count:1,
check:true
},
{
id:4,
name:'《计算机》',
price:10,
count:1,
check:true
},
{
id:5,
name:'《计算机》',
price:10,
count:1,
check:true
},
{
id:6,
name:'《计算机》',
price:10,
count:1,
check:true
}]
}
},
computed:{
totalprice(){
let sum = 0
for(let cart of this.cartList){
if(cart.check){
sum+=cart.price*cart.count
}
}
return sum
}
},
methods:{
del(index){
this.cartList.splice(index,1)
}
}
})
const mv=app.mount('#app')
</script>
</body>
</html>
Vue购物车代码
最新推荐文章于 2024-07-24 17:38:44 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)