当前位置:首页 > VUE

vue实现外卖页面

2026-03-29 15:18:39VUE

实现外卖页面的核心步骤

使用Vue.js实现外卖页面需要结合组件化开发、状态管理以及第三方库的集成。以下为关键实现方法:

页面结构与组件划分

外卖页面通常分为顶部导航栏、商品分类侧边栏、商品列表和底部购物车组件。创建以下Vue组件:

  • Header.vue:包含搜索框和商家信息
  • Sidebar.vue:商品分类导航
  • GoodsList.vue:商品展示卡片
  • Cart.vue:底部购物车汇总
<!-- GoodsList.vue示例 -->
<template>
  <div class="goods-item" v-for="item in goodsList" :key="item.id">
    <img :src="item.image" class="goods-img">
    <div class="goods-info">
      <h3>{{ item.name }}</h3>
      <p class="price">¥{{ item.price }}</p>
      <div class="cart-control">
        <button @click="decrease(item)">-</button>
        <span>{{ item.count || 0 }}</span>
        <button @click="increase(item)">+</button>
      </div>
    </div>
  </div>
</template>

状态管理设计

使用Vuex管理全局状态,包括:

vue实现外卖页面

  • 商品数据
  • 购物车状态
  • 当前分类选择
// store.js
export default new Vuex.Store({
  state: {
    goods: [],
    cart: [],
    currentCategory: 0
  },
  mutations: {
    ADD_TO_CART(state, goods) {
      const record = state.cart.find(item => item.id === goods.id)
      if (!record) {
        state.cart.push({...goods, count: 1})
      } else {
        record.count++
      }
    }
  }
})

交互功能实现

商品分类联动效果通过计算属性和事件监听实现:

// Sidebar.vue
export default {
  computed: {
    categories() {
      return this.$store.state.goods.map(item => item.category)
    }
  },
  methods: {
    selectCategory(index) {
      this.$store.commit('SET_CATEGORY', index)
    }
  }
}

购物车功能优化

实现购物车动画和实时计算:

vue实现外卖页面

// Cart.vue
export default {
  computed: {
    totalPrice() {
      return this.$store.state.cart.reduce((total, item) => {
        return total + item.price * item.count
      }, 0)
    }
  }
}

数据请求与Mock

使用axios获取后端数据,开发阶段可配置Mock数据:

// api.js
export const getGoods = () => {
  return axios.get('/api/goods')
}

// mock数据示例
const mockGoods = [
  {
    id: 1,
    name: '汉堡',
    price: 15,
    category: '快餐'
  }
]

样式与布局技巧

采用flex布局实现响应式设计:

.goods-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.goods-item {
  width: 48%;
  margin-bottom: 15px;
}

性能优化建议

  1. 商品图片使用懒加载
  2. 长列表采用虚拟滚动
  3. 频繁操作的数据使用计算属性缓存
  4. 组件按需加载
// 图片懒加载示例
import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload, {
  loading: require('@/assets/loading.gif')
})

以上实现方案可根据具体需求进行调整,建议结合UI框架如Element UI或Vant加速开发流程。实际项目中还需考虑登录状态、支付流程等完整业务链路的实现。

标签: 外卖页面
分享给朋友:

相关文章

vue实现结算页面

vue实现结算页面

Vue 实现结算页面的核心步骤 数据绑定与表单验证 使用 Vue 的 v-model 绑定表单数据,例如收货地址、支付方式等。通过 Vue 的 computed 属性计算总价,结合 v-if 或 v-…

php实现页面跳转

php实现页面跳转

PHP实现页面跳转的方法 在PHP中,可以通过多种方式实现页面跳转,以下是几种常用的方法: header函数跳转 header("Location: target_page.php"); exit…

vue 实现页面注册

vue 实现页面注册

Vue 实现页面注册功能 在 Vue 中实现用户注册功能通常需要以下步骤: 创建注册表单组件 使用 Vue 的单文件组件结构创建一个注册表单,包含必要的输入字段如用户名、邮箱、密码等。 <t…

vue实现聊天页面

vue实现聊天页面

Vue 实现聊天页面的核心步骤 搭建基础结构 使用 Vue CLI 或 Vite 初始化项目,安装必要依赖如 vue-router 和 axios。创建单文件组件 ChatWindow.vue 作为主…

vue页面实现pdf

vue页面实现pdf

在Vue中实现PDF功能 使用vue-pdf库 安装vue-pdf库: npm install vue-pdf 在Vue组件中使用: <template> <pdf :src…

vue实现页面僵住

vue实现页面僵住

Vue 页面卡顿的可能原因及解决方案 页面卡顿通常由性能瓶颈或代码逻辑问题导致。以下列举常见原因及优化方法: 大量数据渲染导致卡顿 Vue 的响应式系统会对数据进行深度监听,当数据量过大时可能导致渲…