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

<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>

相关推荐
冬枳2 分钟前
搭建一个开箱即用的脚手架
vue.js
天天扭码2 分钟前
DeepSeek V3 震撼升级:AI 生成 HTML/CSS 代码能力实现重大突破
css·html·deepseek
IBELIEVE3 分钟前
前端打包文件本地简易部署
前端
逆袭的小黄鸭4 分钟前
仿 ElementPlus 组件库(九)—— Switch 组件实现
前端·vue.js·typescript
小太阳8216 分钟前
🌟【开源推荐】基于Vue3 + Mango.js的轻量级后台模板Mango Admin发布,RBAC+国际化开箱即用!
vue.js·bun
curdcv_po6 分钟前
Vue 项目线上更新无需强制刷新的方案
前端
dchen7718 分钟前
xhr和fetch的一些区别对比
前端·javascript·面试
进击的松鼠19 分钟前
AI 应用多的我眼花缭乱,不妨做个导航试试看
前端·全栈·next.js
徐小夕19 分钟前
耗资100小时,我开源了一款PPT在线编辑器
前端·javascript·github