用vue3,从0到1搭建一个移动端的项目(一)

一、项目初始化

1. 创建项目

使用Vue CLI创建项目:

javascript 复制代码
# 安装或更新Vue CLI
npm install -g @vue/cli

# 创建项目
vue create vue3-mobile-template

选择Vue3,并根据需要选择以下功能:

  • Babel
  • TypeScript
  • Router
  • Vuex (或Pinia)
  • CSS Pre-processors (SCSS)
  • Linter / Formatter (ESLint + Prettier)

2. 安装核心依赖

javascript 复制代码
# 进入项目目录
cd vue3-mobile-template

# 安装Vant 4.x (适用于Vue3)
npm i vant

# 安装移动端适配方案
npm i amfe-flexible postcss-pxtorem

# 安装axios请求库
npm i axios

# 安装Pinia状态管理(如未在初始化时选择Vuex)
npm i pinia

# 安装工具库
npm i dayjs

二、项目配置

1. 移动端适配配置

创建postcss.config.js文件:

javascript 复制代码
module.exports = {
  plugins: {
    'postcss-pxtorem': {
      rootValue: 37.5, // Vant官方推荐375px设计稿使用37.5
      propList: ['*'],
      selectorBlackList: ['.norem'], // 不转换的类名
      exclude: /node_modules\/(?!(vant))/
    },
  },
};

在main.js中引入flexible:

javascript 复制代码
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
import 'amfe-flexible'

// 导入全局样式
import './assets/styles/index.scss'

const app = createApp(App)
app.use(router)
app.use(createPinia())
app.mount('#app')

2. Vant UI组件按需引入

使用unplugin-vue-components实现自动按需引入:

javascript 复制代码
npm i unplugin-vue-components -D

配置vue.config.js:

js 复制代码
const { defineConfig } = require('@vue/cli-service')
const { VantResolver } = require('unplugin-vue-components/resolvers')
const ComponentsPlugin = require('unplugin-vue-components/webpack')

module.exports = defineConfig({
  transpileDependencies: true,
  configureWebpack: {
    plugins: [
      ComponentsPlugin({
        resolvers: [VantResolver()],
      }),
    ],
  },
  // 开发服务器配置
  devServer: {
    port: 3000,
    open: true,
    proxy: {
      '/api': {
        target: 'http://your-api-domain.com',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  }
})

三、项目结构搭建

python 复制代码
src/
├── api/                # API请求模块
│   ├── index.js        # API接口统一出口
│   ├── user.js         # 用户相关接口
│   └── common.js       # 公共接口
├── assets/             # 静态资源
│   ├── images/         # 图片资源
│   ├── icons/          # 图标资源
│   └── styles/         # 样式文件
│       ├── index.scss  # 全局样式入口
│       ├── reset.scss  # 重置样式
│       └── variables.scss # 样式变量
├── components/         # 公共组件
│   ├── common/         # 通用组件
│   └── business/       # 业务组件
├── router/             # 路由配置
│   ├── index.js        # 路由入口
│   └── routes.js       # 路由表
├── stores/             # Pinia状态管理
│   ├── index.js        # 状态管理入口
│   └── modules/        # 状态模块
│       ├── user.js     # 用户状态
│       └── app.js      # 应用状态
├── utils/              # 工具函数
│   ├── index.js        # 工具函数入口
│   ├── request.js      # Axios请求封装
│   ├── storage.js      # 本地存储封装
│   ├── validate.js     # 表单验证
│   └── format.js       # 格式化工具
├── views/              # 页面组件
│   ├── home/           # 首页
│   ├── user/           # 用户页面
│   └── error/          # 错误页面
├── App.vue             # 根组件
├── main.js             # 入口文件
└── permission.js       # 权限控制
相关推荐
Mr.Jessy2 小时前
JavaScript高级:构造函数与原型
开发语言·前端·javascript·学习·ecmascript
白兰地空瓶4 小时前
🚀你以为你在写 React?其实你在“搭一套前端操作系统”
前端·react.js
爱上妖精的尾巴5 小时前
6-4 WPS JS宏 不重复随机取值应用
开发语言·前端·javascript
似水流年QC5 小时前
深入探索 WebHID:Web 标准下的硬件交互实现
前端·交互·webhid
陪我去看海5 小时前
测试 mcp
前端
speedoooo6 小时前
在现有App里嵌入一个AI协作者
前端·ui·小程序·前端框架·web app
全栈胖叔叔-瓜州6 小时前
关于llamasharp 大模型多轮对话,模型对话无法终止,或者输出角色标识User:,或者System等角色标识问题。
前端·人工智能
三七吃山漆6 小时前
攻防世界——wife_wife
前端·javascript·web安全·网络安全·ctf
用户47949283569156 小时前
面试官问"try-catch影响性能吗",我用数据打脸
前端·javascript·面试
GISer_Jing7 小时前
前端营销技术实战:数据+AI实战指南
前端·javascript·人工智能