本文实例为大家分享了Vue实现本地购物车功能的具体代码,供大家参考,具体内容如下
功能分析 : v-for显示商品名字,价格,数量和对商品进行操作,全选的功能
结构仍然分成 : index.html , index.js , style.css
index.html
购物车实例 总价 : ¥{{totalPrice}} 购物车为空
序号 商品名称 商品单价 购买数量 操作 {{index + 1}} {{item.name}} {{item.price}} {{item.count}}
index.js
const app = new Vue({
el : '#app',
data: {
allCheck:false,
list : [
{
id: 1 ,
name :'iPhone 8 ',
price: 6188 ,
count: 1 ,
isChecked : false
},
{
id: 2 ,
name :'小米 8 ',
price: 5888 ,
count: 1 ,
isChecked : false
},
{
id: 3 ,
name :'iPad Pro ',
price: 11000 ,
count: 1 ,
isChecked : false
},
{
id: 4 ,
name :'雷神SE9',
price: 6188 ,
count: 10 ,
isChecked : false
},
]
},
computed : {
//通过计算属性获取总价格
totalPrice:function() {
let total = 0;
const newArr = this.list.filter(value => {
return value.isChecked == true
})
for(var i = 0 ;i {
value.isChecked = this.allCheck
})
},
//单选,当全部选中时,改变全选按钮的状态
singleCheck(index) {
this.list[index].isChecked = !this.list[index].isChecked
const selectData = this.list.filter(value => {
return value.isChecked == true
})
this.allCheck = selectData.length === this.list.length ? true : false
}
}
})
style.css
[v-cloak] {
display: none;
}
table {
border: 1px solid #e9e9e9;
border-collapse: collapse;
border-spacing: 0;
empty-cells: show;
}
th,td {
padding: 8px 16px;
border: 1px solid #e9e9e9;
text-align: left;
}
th {
background: yellowgreen;
color: #5c6b77;
font-weight: 600;
white-space: nowrap;
}
效果图如下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



