vue实现外卖app
Vue 实现外卖 App 的核心功能
外卖 App 的核心功能包括商品展示、购物车管理、订单流程和用户交互。Vue.js 的响应式特性和组件化开发非常适合实现这些功能。
项目结构搭建
使用 Vue CLI 快速初始化项目,安装必要的依赖如 Vue Router 和 Vuex。项目目录应包含 components、views、store 和 router 等标准文件夹。
vue create food-delivery-app
cd food-delivery-app
npm install vue-router vuex
路由配置
在 router/index.js 中配置基本路由,包括首页、商家列表、商家详情、购物车和订单页。
const routes = [
{ path: '/', component: Home },
{ path: '/restaurants', component: RestaurantList },
{ path: '/restaurant/:id', component: RestaurantDetail },
{ path: '/cart', component: ShoppingCart },
{ path: '/order', component: OrderConfirm }
]
状态管理
使用 Vuex 管理全局状态,如购物车数据、用户信息和订单历史。创建 store/modules/cart.js 处理购物车相关逻辑。
const state = {
items: [],
total: 0
}
const mutations = {
ADD_ITEM(state, item) {
state.items.push(item)
state.total += item.price
}
}
商家列表实现
创建 RestaurantList 组件,使用 axios 获取后端 API 的商家数据。为每个商家卡片添加点击事件,跳转到详情页。

<template>
<div v-for="restaurant in restaurants" :key="restaurant.id">
<restaurant-card :data="restaurant" @click="goToDetail(restaurant.id)"/>
</div>
</template>
商品详情页
在 RestaurantDetail 组件中展示菜单分类和具体商品。实现加入购物车功能,触发 Vuex 的 ADD_ITEM mutation。
methods: {
addToCart(item) {
this.$store.commit('ADD_ITEM', item)
}
}
购物车功能
购物车组件显示已选商品列表和总价。实现数量增减和删除功能,实时计算总金额。
<template>
<div v-for="item in cartItems" :key="item.id">
<span>{{ item.name }}</span>
<button @click="decreaseQuantity(item)">-</button>
<span>{{ item.quantity }}</span>
<button @click="increaseQuantity(item)">+</button>
</div>
</template>
订单确认页
收集用户配送信息和支付方式,提交订单到后端。使用表单验证确保必填字段完整。

submitOrder() {
if (this.validateForm()) {
axios.post('/api/orders', this.orderData)
.then(response => {
this.$router.push('/order-success')
})
}
}
移动端适配
添加 viewport meta 标签确保移动端显示正常。使用 flex 布局和媒体查询适应不同屏幕尺寸。
<meta name="viewport" content="width=device-width, initial-scale=1.0">
性能优化
对商品图片使用懒加载,路由组件异步加载。使用 keep-alive 缓存常用页面提升用户体验。
const RestaurantDetail = () => import('./views/RestaurantDetail.vue')
项目部署
构建生产环境代码并部署到服务器。配置 Nginx 处理前端路由的重定向。
npm run build
以上实现方案涵盖了外卖 App 的主要功能模块,开发者可根据实际需求扩展更多功能如评价系统、优惠券和定位服务等。






