本文实例为大家分享了Vue简单实现购物车结算的具体代码,供大家参考,具体内容如下 样式没有写 <template> <div class="about cont
本文实例为大家分享了Vue简单实现购物车结算的具体代码,供大家参考,具体内容如下
样式没有写
<template>
<div class="about container">
<button class="btn btn-default">
<router-link to="/customers">返回主页</router-link>
</button>
<button>获取数据</button>
<ul>
<li v-for="(item,index) in this.list" :key="index">
<div>
<input type="checkbox" v-model="item.checked" />
<span>{{item.title}}</span>
<button class="btn btn-default" @click="reduce(index)">-</button>
<span>{{item.num}}</span>
<button class="btn btn-default" @click="add(index)">+</button>
<span>价格:{{item.price}}</span>
<button @click="del(index,item._id)" class="btn btn-default">删除</button>
</div>
</li>
</ul>
<input type="checkbox" v-model="allcheck" @click="checkall" />
<button>合计:{{totalMoney}}</button>
</div>
</template>
<script>
export default {
name: "about",
// 调用app.vue组件中的方法
inject: ["reload"],
data() {
return {
list: [],
allcheck: false
};
},
methods: {
async getproduct() {
const res = await this.$Http.get("/Goodslist");
this.list = res.data;
localStorage.setItem("shoppinGCart", JSON.stringify(this.list));
console.log(this.list);
},
add(i) {
this.list[i].num++;
},
reduce(i) {
if (this.list[i].num <= 1) {
this.list[i].num = 0;
return;
}
this.list[i].num--;
},
checkall() {
// console.log(this.allcheck);
console.log(event.target.checked);
this.list.forEach(item => {
item.checked = event.target.checked;
// console.log(item._id)
});
},
async del(i, id) {
const res = await this.$http.delete("/delfile/" + id);
console.log(res);
this.reload();
}
},
computed: {
totalMoney() {
let allmoney = 0;
let isAllCheck = true;
for (let i in this.list) {
if (this.list[i].checked) {
allmoney += this.list[i].price * this.list[i].num;
} else {
isAllCheck = false;
}
}
this.allcheck == isAllCheck;
return allmoney;
}
},
created() {
this.getproduct();
}
};
</script>
<style>
li {
list-style: none;
}
</style>
--结束END--
本文标题: vue简单实现购物车结算功能
本文链接: https://lsjlt.com/news/145903.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0