当前位置:首页 > 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>

样式优化

为加减按钮添加样式:

vue实现点菜加减

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 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含表单元素,如用户名和密码输入框,以及提交按钮。 <template&…

vue实现收起展开面板

vue实现收起展开面板

实现收起展开面板的方法 在Vue中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起:…

vue实现建模

vue实现建模

Vue 实现建模的方法 在 Vue 中实现建模通常涉及数据绑定、组件化和状态管理。以下是几种常见的方法: 数据驱动建模 Vue 的核心是数据驱动视图。通过定义数据模型,Vue 会自动更新 DOM。例…

vue实现异步

vue实现异步

Vue 实现异步操作的常见方法 Vue 提供了多种方式来处理异步操作,包括数据获取、事件处理等场景。 使用 async/await 在 Vue 方法中可以直接使用 async/await 语法处理异…

vue实现海报

vue实现海报

Vue 实现海报生成方案 使用 html2canvas 库 html2canvas 是一个将 HTML 元素转换为 Canvas 的库,适合将 Vue 组件渲染为海报图片 安装依赖: npm in…

vue实现datalist

vue实现datalist

使用 Vue 实现 HTML5 的 datalist 功能 HTML5 的 <datalist> 元素提供了一种自动完成输入框的功能,允许用户在输入时从预定义的选项列表中选择。以下是几种在…