当前位置:首页 > VUE

vue实现点菜加减

2026-03-10 06:13:51VUE

实现点菜加减功能

在Vue中实现点菜加减功能,可以通过数据绑定和事件处理来完成。以下是具体实现方法:

数据准备 在Vue组件的data中定义菜品列表和数量:

data() {
  return {
    dishes: [
      { id: 1, name: '红烧肉', price: 38, quantity: 0 },
      { id: 2, name: '清蒸鱼', price: 48, quantity: 0 },
      { id: 3, name: '宫保鸡丁', price: 32, quantity: 0 }
    ]
  }
}

模板渲染 使用v-for循环渲染菜品列表,并绑定加减按钮事件:

vue实现点菜加减

<div v-for="dish in dishes" :key="dish.id" class="dish-item">
  <span>{{ dish.name }} - ¥{{ dish.price }}</span>
  <button @click="decrease(dish)">-</button>
  <span>{{ dish.quantity }}</span>
  <button @click="increase(dish)">+</button>
</div>

方法实现 在methods中定义加减方法:

methods: {
  increase(dish) {
    dish.quantity++
  },
  decrease(dish) {
    if (dish.quantity > 0) {
      dish.quantity--
    }
  }
}

添加计算总价功能

可以添加一个计算属性来计算总价:

vue实现点菜加减

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

在模板中显示总价:

<div class="total-price">
  总价: ¥{{ totalPrice }}
</div>

样式优化

添加基本样式美化界面:

.dish-item {
  margin: 10px;
  padding: 10px;
  border: 1px solid #eee;
  display: flex;
  align-items: center;
}
.dish-item button {
  margin: 0 5px;
  padding: 2px 10px;
}
.total-price {
  margin-top: 20px;
  font-weight: bold;
  font-size: 18px;
}

完整组件示例

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

<script>
export default {
  data() {
    return {
      dishes: [
        { id: 1, name: '红烧肉', price: 38, quantity: 0 },
        { id: 2, name: '清蒸鱼', price: 48, quantity: 0 },
        { id: 3, name: '宫保鸡丁', price: 32, quantity: 0 }
      ]
    }
  },
  methods: {
    increase(dish) {
      dish.quantity++
    },
    decrease(dish) {
      if (dish.quantity > 0) {
        dish.quantity--
      }
    }
  },
  computed: {
    totalPrice() {
      return this.dishes.reduce((sum, dish) => {
        return sum + dish.price * dish.quantity
      }, 0)
    }
  }
}
</script>

<style>
.dish-item {
  margin: 10px;
  padding: 10px;
  border: 1px solid #eee;
  display: flex;
  align-items: center;
}
.dish-item button {
  margin: 0 5px;
  padding: 2px 10px;
}
.total-price {
  margin-top: 20px;
  font-weight: bold;
  font-size: 18px;
}
</style>

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

相关文章

vue查看更多怎么实现

vue查看更多怎么实现

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

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

vue实现登出

vue实现登出

实现Vue登出功能 登出功能通常涉及清除用户会话、令牌或本地存储的数据,并重定向到登录页面。以下是几种常见的实现方式: 清除用户令牌和状态 在Vuex的store中定义一个logout mutati…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue路由实现

vue路由实现

Vue 路由实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的基本实现步骤和核心功能。 安装 Vue Router 通过…