当前位置:首页 > VUE

vue实现移动商城

2026-01-18 01:27:06VUE

Vue 实现移动商城的核心步骤

项目初始化与配置

使用 Vue CLI 创建项目,安装必要依赖:

vue create mobile-mall
cd mobile-mall
npm install vue-router vuex vant axios --save

配置 vant 按需引入,在 babel.config.js 中添加:

module.exports = {
  plugins: [
    ['import', {
      libraryName: 'vant',
      libraryDirectory: 'es',
      style: true
    }, 'vant']
  ]
}

基础架构搭建

创建路由文件 src/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/views/Home.vue'

Vue.use(Router)

export default new Router({
  routes: [
    { path: '/', component: Home },
    { path: '/category', component: () => import('@/views/Category.vue') },
    { path: '/cart', component: () => import('@/views/Cart.vue') },
    { path: '/mine', component: () => import('@/views/Mine.vue') }
  ]
})

配置 Vuex 状态管理 src/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cartList: []
  },
  mutations: {
    addToCart(state, item) {
      state.cartList.push(item)
    }
  }
})

移动端适配方案

安装 postcss 插件:

npm install postcss-pxtorem --save-dev

配置 postcss.config.js

module.exports = {
  plugins: {
    'postcss-pxtorem': {
      rootValue: 37.5,
      propList: ['*']
    }
  }
}

核心页面实现

首页布局示例 (Home.vue)

<template>
  <div class="home">
    <van-swipe :autoplay="3000">
      <van-swipe-item v-for="(item, index) in banners" :key="index">
        <img :src="item.image" />
      </van-swipe-item>
    </van-swipe>

    <van-grid :column-num="4">
      <van-grid-item 
        v-for="(item, index) in categories" 
        :key="index"
        :icon="item.icon"
        :text="item.name"
      />
    </van-grid>
  </div>
</template>

<script>
export default {
  data() {
    return {
      banners: [
        { image: 'https://img01.yzcdn.cn/vant/apple-1.jpg' },
        { image: 'https://img01.yzcdn.cn/vant/apple-2.jpg' }
      ],
      categories: [
        { name: '分类1', icon: 'photo-o' },
        { name: '分类2', icon: 'photo-o' }
      ]
    }
  }
}
</script>

商品详情页实现

<template>
  <div class="detail">
    <van-image :src="product.image" />
    <van-cell-group>
      <van-cell title="商品名称" :value="product.name" />
      <van-cell title="商品价格" :value="'¥' + product.price" />
    </van-cell-group>

    <van-submit-bar 
      :price="product.price*100" 
      button-text="加入购物车"
      @submit="addToCart"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      product: {
        id: 1,
        name: '示例商品',
        price: 99,
        image: 'https://img01.yzcdn.cn/vant/apple-3.jpg'
      }
    }
  },
  methods: {
    addToCart() {
      this.$store.commit('addToCart', this.product)
      this.$toast.success('添加成功')
    }
  }
}
</script>

购物车功能实现

<template>
  <div class="cart">
    <van-checkbox-group v-model="checked">
      <van-swipe-cell v-for="(item, index) in cartList" :key="index">
        <van-checkbox :name="item.id" />
        <van-card
          :num="item.count"
          :price="item.price"
          :desc="item.desc"
          :title="item.name"
          :thumb="item.image"
        />
      </van-swipe-cell>
    </van-checkbox-group>

    <van-submit-bar
      :price="totalPrice"
      button-text="提交订单"
      @submit="onSubmit"
    />
  </div>
</template>

<script>
export default {
  computed: {
    cartList() {
      return this.$store.state.cartList
    },
    totalPrice() {
      return this.cartList.reduce((sum, item) => sum + item.price*100, 0)
    }
  },
  methods: {
    onSubmit() {
      this.$dialog.confirm({
        title: '确认订单',
        message: '确认支付吗?'
      })
    }
  }
}
</script>

移动端优化技巧

添加 viewport meta 标签:

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

使用 rem 布局时,在 main.js 添加:

document.documentElement.style.fontSize = window.innerWidth / 10 + 'px'
window.addEventListener('resize', () => {
  document.documentElement.style.fontSize = window.innerWidth / 10 + 'px'
})

项目部署

构建生产环境代码:

npm run build

配置 nginx 服务器:

vue实现移动商城

server {
  listen 80;
  server_name yourdomain.com;
  root /path/to/dist;
  index index.html;

  location / {
    try_files $uri $uri/ /index.html;
  }
}

扩展功能建议

  • 集成支付 SDK(如微信/支付宝)
  • 添加商品搜索功能
  • 实现用户评价系统
  • 增加优惠券模块
  • 开发后台管理系统对接 API

以上实现方案采用 Vue 2.x 语法,如需使用 Vue 3,需相应调整组合式 API 写法。实际开发中应对接真实后端 API 接口,示例中的静态数据仅作演示用途。

标签: 商城vue
分享给朋友:

相关文章

vue实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <template&g…

vue实现积分

vue实现积分

Vue 实现积分功能 在 Vue 中实现积分功能通常涉及前端展示、交互逻辑以及与后端 API 的通信。以下是常见的实现方法和步骤: 积分展示组件 创建一个积分展示组件,用于显示当前用户的积分。可以使…

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

vue原理实现

vue原理实现

Vue 原理实现的核心机制 Vue.js 的核心原理基于响应式系统、虚拟 DOM 和模板编译。以下是其核心实现机制的分解: 响应式系统 Vue 使用 Object.defineProperty(Vu…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…

vue核心实现

vue核心实现

Vue 核心实现原理 Vue.js 的核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是关键实现原理的详细说明: 响应式系统 Vue 使用 Object.defineProper…