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

加减方法实现

vue实现点菜加减

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>

计算总价功能

添加计算总价的方法:

vue实现点菜加减

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实现ai

vue实现ai

Vue 实现 AI 功能 Vue.js 可以通过集成第三方 AI 服务或本地 AI 模型来实现 AI 功能。以下是几种常见的方法: 集成第三方 AI API 使用 Vue 调用如 OpenAI、Go…

vue实现markdown

vue实现markdown

Vue 实现 Markdown 编辑器 在 Vue 中实现 Markdown 编辑器可以通过集成第三方库或自定义解析器完成。以下是两种常见方法: 使用第三方库(如 marked 或 markdown…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…

vue 实现报价

vue 实现报价

Vue 实现报价功能 在 Vue 中实现报价功能通常涉及前端表单交互、数据计算和与后端 API 的通信。以下是实现报价功能的几种常见方法: 表单设计与数据绑定 使用 Vue 的 v-model 指…

vue实现展开

vue实现展开

展开功能的实现方法 在Vue中实现展开功能可以通过多种方式完成,常见的有使用v-show、v-if指令,结合CSS过渡动画或第三方组件库。 使用v-show控制显示隐藏 通过v-show指令可以简单…

vue 实现templateurl

vue 实现templateurl

在 Vue 中,templateUrl 是 Angular 中的一个概念,用于指定外部 HTML 文件作为模板。Vue 本身不直接支持 templateUrl,但可以通过以下方法实现类似功能: 使用…