在项目中写一个购物车功能

<template>

<div class="two">

<div v-for="item in taskPriceList" :key="item.id">

<input type="radio" value="selectInput" v-model="item.selectF" />

<p>¥{{item.price}}元</p>

<div>

<button @click="delBtn(item.id)">-</button>

<span class="span">{{item.count}}</span>

<button @click="addBtn(item.id)">+</button>

</div>

</div>

<p>总共合计:{{totalPrice}}元</p>

</div>

</template>

<script>

export default {

name: "ShengMingZQTwo",

data() {

return {

taskPriceList: [

{ id: 1, selectF: "", count: 1, price: 138 },

{ id: 2, selectF: "", count: 2, price: 30 },

{ id: 3, selectF: "", count: 1, price: 450 }

]

};

},

computed: {

totalPrice() {

let sum = 0;

this.taskPriceList.forEach(item => {

if (item.selectF == "selectInput") {

sum += item.price * item.count;

}

});

return sum;

}

},

methods: {

addBtn(value) {

this.taskPriceList.forEach(item => {

if (value == item.id) {

item.count++;

item.selectF = "selectInput";

}

});

},

delBtn(value) {

this.taskPriceList.forEach(item => {

if (value == item.id && item.count > 1) {

item.count--;

if (item.count == 1) {

item.selectF = "";

}

}

});

}

}

};

</script>

<style scoped>

.span {

display: inline-block;

margin: 0 18px;

}

</style>

相关推荐
search72 小时前
前端设计:CRG 3--CDC error
前端
治金的blog2 小时前
vben-admin和vite,ant-design-vue的结合的联系
前端·vscode
利刃大大3 小时前
【Vue】Vue2 和 Vue3 的区别
前端·javascript·vue.js
荔枝一杯酸牛奶4 小时前
HTML 表单与表格布局实战:两个经典作业案例详解
前端·html
Charlie_lll5 小时前
学习Three.js–纹理贴图(Texture)
前端·three.js
yuguo.im5 小时前
我开源了一个 GrapesJS 插件
前端·javascript·开源·grapesjs
安且惜5 小时前
带弹窗的页面--以表格形式展示
前端·javascript·vue.js
米奇妙妙wuu5 小时前
css实现文字和边框同样的渐变色效果
css·html·css3
GISer_Jing6 小时前
AI驱动营销:业务技术栈实战(From AIGC,待总结)
前端·人工智能·aigc·reactjs
GIS之路8 小时前
GDAL 实现影像裁剪
前端·python·arcgis·信息可视化