当前位置:首页 > 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 服务器:

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实现拖拽

Vue 实现拖拽功能 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来实现。以下是两种常见方法的详细说明: 使用 HTML5 拖拽 AP…

vue响应式实现

vue响应式实现

Vue 响应式实现原理 Vue 的响应式系统基于 Object.defineProperty(Vue 2.x)或 Proxy(Vue 3.x)实现,通过数据劫持和依赖收集实现数据变化时的自动更新。…

订单 vue实现

订单 vue实现

Vue 实现订单功能 数据模型设计 订单功能通常需要设计以下数据结构: // 订单数据结构 const order = { id: '', // 订单ID userId…

vue查看更多怎么实现

vue查看更多怎么实现

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

vue实现微博印象

vue实现微博印象

实现微博印象功能的核心思路 微博印象功能通常指用户发布微博后,系统自动分析文本内容并提取关键词或标签,以可视化形式展示。Vue实现该功能需要结合文本分析、关键词提取和前端渲染技术。 文本分析与关键…

vue前端实现打印功能

vue前端实现打印功能

使用Vue实现前端打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 使用window.print()方法 这是最简单的打印方式,直接调用浏览器的打印功能。 meth…