当前位置:首页 > VUE

vue实现点菜加减

2026-01-18 00:28:22VUE

实现点菜加减功能

在Vue中实现点菜加减功能,通常需要以下步骤:

数据模型设计

data() {
  return {
    dishes: [
      { id: 1, name: '鱼香肉丝', price: 28, count: 0 },
      { id: 2, name: '宫保鸡丁', price: 32, count: 0 }
    ]
  }
}

加减方法实现

methods: {
  increaseCount(dish) {
    dish.count++
  },
  decreaseCount(dish) {
    if (dish.count > 0) {
      dish.count--
    }
  }
}

模板部分

<div v-for="dish in dishes" :key="dish.id">
  <span>{{ dish.name }}</span>
  <span>¥{{ dish.price }}</span>
  <button @click="decreaseCount(dish)">-</button>
  <span>{{ dish.count }}</span>
  <button @click="increaseCount(dish)">+</button>
</div>

计算总价功能

添加计算总价的方法:

computed: {
  totalPrice() {
    return this.dishes.reduce((sum, dish) => {
      return sum + dish.price * dish.count
    }, 0)
  }
}

在模板中显示总价:

<div>总价:¥{{ totalPrice }}</div>

样式优化

为加减按钮添加样式:

button {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid #ccc;
  background: #fff;
  cursor: pointer;
}

button:hover {
  background: #f5f5f5;
}

完整组件示例

<template>
  <div class="menu">
    <div v-for="dish in dishes" :key="dish.id" class="dish-item">
      <span class="name">{{ dish.name }}</span>
      <span class="price">¥{{ dish.price }}</span>
      <button @click="decreaseCount(dish)">-</button>
      <span class="count">{{ dish.count }}</span>
      <button @click="increaseCount(dish)">+</button>
    </div>
    <div class="total">总价:¥{{ totalPrice }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dishes: [
        { id: 1, name: '鱼香肉丝', price: 28, count: 0 },
        { id: 2, name: '宫保鸡丁', price: 32, count: 0 },
        { id: 3, name: '水煮鱼', price: 45, count: 0 }
      ]
    }
  },
  methods: {
    increaseCount(dish) {
      dish.count++
    },
    decreaseCount(dish) {
      if (dish.count > 0) {
        dish.count--
      }
    }
  },
  computed: {
    totalPrice() {
      return this.dishes.reduce((sum, dish) => {
        return sum + dish.price * dish.count
      }, 0)
    }
  }
}
</script>

<style>
.menu {
  width: 300px;
  margin: 20px auto;
}
.dish-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
.name {
  flex: 2;
}
.price {
  flex: 1;
  text-align: right;
  margin-right: 15px;
}
.count {
  width: 30px;
  text-align: center;
}
button {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid #ccc;
  background: #fff;
  cursor: pointer;
}
button:hover {
  background: #f5f5f5;
}
.total {
  margin-top: 20px;
  text-align: right;
  font-weight: bold;
}
</style>

这个实现包含了基本的点菜加减功能,并提供了总价计算和简单样式。可以根据实际需求进一步扩展功能,如添加菜品图片、分类显示等。

vue实现点菜加减

标签: 加减vue
分享给朋友:

相关文章

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPa…

vue查看更多怎么实现

vue查看更多怎么实现

实现 Vue 的“查看更多”功能 数据截取与显示控制 通过 v-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&…

vue前端实现打印功能

vue前端实现打印功能

使用Vue实现前端打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 使用window.print()方法 这是最简单的打印方式,直接调用浏览器的打印功能。 me…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…