结果如下图所示:
源码如下(可直接复制使用):
<!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">
<link rel="stylesheet" href="./bootstrap.min.css">
<script src="../../js/vue.js"></script>
<title>购物车</title>
<style>
* {
margin: 0 auto;
padding: 0;
}
.top,
.footer {
width: 500px;
height: 50px;
background-color: burlywood;
text-align: center;
line-height: 50px;
}
.cart .footer {
position: relative;
}
.cart .footer .count {
position: absolute;
height: 25px;
line-height: 11px;
right: 20px;
top: 12px;
font-weight: 800;
}
.cart .footer .allprice {
position: absolute;
right: 100px;
}
.cart .footer .allprice span {
font-weight: 800;
}
.cart .table thead {
font-weight: 800;
}
.cart .table {
width: 500px;
height: 300px;
text-align: center;
}
.table tbody tr td {
height: 50px;
line-height: 50px;
}
.table tbody tr td:nth-child(5) {
font-size: 25px;
}
.table tbody tr td:nth-child(5) a {
text-decoration: none;
}
.table tbody tr td:nth-child(4) input {
width: 20px;
height: 20px;
border: none;
}
.table tbody tr td span {
width: 20px;
height: 20px;
text-align: center;
font-weight: 900;
}
.table img {
width: 50px;
}
</style>
</head>
<body>
<div id="app">
<my-cart></my-cart>
</div>
<script>
var CartTitle = {
props: ['uname'],
template: `
<div class="top">{{uname}}的商品</div>
`
}
var CartList = {
props: ['list'],
template: `
<div class="main">
<table class="table">
<thead>
<tr>
<td>id</td>
<td>商品</td>
<td>单价</td>
<td>数量</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<tr :key='item.id' v-for='item in list'>
<td>{{item.id}}</td>
<td><img :src="item.img" alt=""></td>
<td>¥{{item.price}}</td>
<td><span class="glyphicon glyphicon-minus" @click='sub(item.id)'></span> <input type="text" :value="item.num" @blur='changeNum(item.id,$event)'> <span
class="glyphicon glyphicon-plus" @click='add(item.id)'></span>
</td>
<td><a href="#" @click='del(item.id)'>×</a></td>
</tr>
</tbody>
</table>
</div>
`,
methods: {
changeNum: function (id, event) {
this.$emit('change-num', {
id: id,
num: event.target.value,
type: 'change'
});
},
sub: function (id) {
this.$emit('change-num', {
id: id,
type: 'sub',
})
},
add: function (id) {
this.$emit('change-num', {
id: id,
type: 'add',
})
},
del: function (id) {
this.$emit('cart-del', id)
}
}
}
var CartTotal = {
props: ['list'],
template: `
<div class="footer">
<div class="allprice">
<span>总计:</span>
<span>¥{{total}}</span>
</div>
<button class="btn btn-default count">结算</button>
</div>
`,
// 计算属性
computed: {
total: function () {
var t = 0;
this.list.forEach(item => {
t += item.price * item.num;
});
return t;
}
}
}
Vue.component('my-cart', {
data: function () {
return {
uname: 'jack',
list: [
{
id: 1,
img: './image/phone1.jpg',
price: 2999,
num: 1
},
{
id: 2,
img: './image/phone2.jpg',
price: 1999,
num: 1
},
{
id: 3,
img: './image/phone3.jpg',
price: 3999,
num: 2
},
]
}
},
template:
`
<div class="cart">
<cart-title :uname='uname'></cart-title>
<cart-list :list='list' @change-num='changeNum($event)' @cart-del='delCart($event)'></cart-list>
<cart-total :list='list'></cart-total>
</div>
`,
components: {
'cart-title': CartTitle,
'cart-list': CartList,
'cart-total': CartTotal,
},
methods: {
changeNum: function (val) {
// 分三种情况:作用域变更,加法操作,减法操作
// console.log(val);
if (val.type == 'change') {
this.list.some(item => {
if (item.id == val.id) {
item.num = val.num;
// 终止遍历 some方法的规则
return true;
}
})
// 减法操作
}else if(val.type =='sub'){
this.list.some(item => {
if (item.id == val.id && item.num>1) {
item.num -=1;
// 终止遍历 some方法的规则
return true;
}
})
// 加法操作
}else if(val.type=='add'){
this.list.some(item => {
if (item.id == val.id) {
item.num +=1;
// 终止遍历 some方法的规则
return true;
}
})
}
},
delCart: function (id) {
// console.log(id);
// 1.找到当前id对应的索引
var index = this.list.findIndex(item => {
return item.id = id;
})
// 2.根据索引删除对应的数据
this.list.splice(index, 1);
}
}
})
var vm = new Vue({
el: '#app',
data: {
}
})
</script>
</body>
</html>
大家不懂的地方,或者copy之后不能运行,可评论区留言,希望这篇文章对你们有所帮助哦~