当前位置:首页 > VUE

vue实现外卖页面

2026-03-09 03:47:38VUE

实现外卖页面的核心步骤

使用Vue实现外卖页面需要结合组件化开发、状态管理、路由和API交互。以下是关键实现方法:

页面结构与组件划分

外卖页面通常分为顶部导航栏、商家信息区、商品分类区、购物车和底部结算栏。Vue组件可划分为:

  • Header:顶部导航和搜索
  • ShopInfo:商家Logo、名称、评分等信息
  • GoodsCategory:商品分类侧边栏
  • GoodsList:商品列表展示
  • Cart:购物车浮层
  • Checkout:底部结算栏

组件通信使用Vuex管理全局状态,如购物车数据、当前选中分类等。

商品数据获取与渲染

通过axios获取后端API数据,典型数据结构示例:

// 商品分类数据结构
categories: [
  {
    id: 1,
    name: "套餐",
    foods: [
      {
        id: 101,
        name: "汉堡套餐",
        price: 25,
        sales: 100,
        description: "含汉堡+薯条+可乐"
      }
    ]
  }
]

使用v-for渲染分类和商品列表:

<div v-for="category in categories" :key="category.id">
  <h3>{{ category.name }}</h3>
  <div v-for="food in category.foods" :key="food.id">
    {{ food.name }} - ¥{{ food.price }}
  </div>
</div>

购物车功能实现

Vuex store示例:

vue实现外卖页面

state: {
  cartItems: []
},
mutations: {
  addToCart(state, food) {
    const existing = state.cartItems.find(item => item.id === food.id)
    existing ? existing.quantity++ : state.cartItems.push({...food, quantity: 1})
  }
}

组件内调用:

methods: {
  addFood(food) {
    this.$store.commit('addToCart', food)
  }
}

交互优化技巧

实现分类联动滚动:

// 监听滚动位置匹配当前分类
window.addEventListener('scroll', () => {
  const scrollTop = document.documentElement.scrollTop
  this.categories.forEach((cat, index) => {
    const el = document.getElementById(`cat-${cat.id}`)
    if (el.offsetTop <= scrollTop + 100) {
      this.currentCategory = index
    }
  })
})

移动端适配要点

  1. 使用viewport meta标签:

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  2. 添加点击延迟解决:

    vue实现外卖页面

    npm install fastclick
    import FastClick from 'fastclick'
    FastClick.attach(document.body)
  3. 使用flex布局适配不同屏幕:

    .goods-item {
    display: flex;
    padding: 12px;
    }

性能优化建议

  • 商品图片懒加载:

    <img v-lazy="food.image">
  • 长列表使用虚拟滚动:

    npm install vue-virtual-scroller
  • 路由懒加载:

    const Goods = () => import('./components/Goods.vue')

完整项目结构参考

src/
├── api/
│   └── shop.js        # 接口请求
├── components/
│   ├── Header.vue
│   ├── ShopInfo.vue
│   └── Cart.vue
├── store/
│   ├── index.js       # Vuex主文件
│   └── modules/       # 模块化store
├── views/
│   └── Shop.vue       # 主页面
└── main.js

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

相关文章

vue实现 页面

vue实现 页面

Vue 实现页面的基本方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现页面的常见方法: 单文件组件(SFC) 使用 .vue 文件组织页面结构,包含模板、脚…

vue实现页面切换

vue实现页面切换

Vue 实现页面切换的方法 Vue 中实现页面切换通常使用 Vue Router,这是 Vue.js 官方的路由管理器。以下是几种常见的实现方式: 使用 Vue Router 的基本配置 安装 Vu…

js实现跳转页面

js实现跳转页面

使用 window.location.href 实现跳转 通过修改 window.location.href 属性可以跳转到指定 URL。这是最常见且简单的方法,会触发页面刷新并加载新页面。 win…

vue实现页面缩略

vue实现页面缩略

Vue实现页面缩略的方法 使用CSS的transform: scale() 通过CSS的transform属性可以实现页面缩放效果。在Vue组件中,可以动态绑定样式来控制缩放比例。 <temp…

vue  实现页面返回

vue 实现页面返回

Vue 实现页面返回功能 在Vue中实现页面返回功能,可以通过以下几种方式实现: 使用路由的go方法 通过Vue Router的go方法可以控制页面跳转,传入负数表示后退。 this.$rout…

vue实现音乐页面

vue实现音乐页面

使用 Vue 实现音乐页面 项目初始化 使用 Vue CLI 或 Vite 创建一个新的 Vue 项目。确保安装了必要的依赖,如 vue-router 和 axios。 npm create vue…