当前位置:首页 > 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中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

vue插槽实现

vue插槽实现

插槽的基本概念 Vue插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽 子…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…