赞
踩
如何使用前端技术搭建一个移动端项目。我们将使用Vue3作为前端框架,Vite作为构建工具,以及Vant作为移动端UI组件库。
兼容性:Vite 需要 Node.js 版本 >= 12.0.0。
npm init vite@latest
yarn create vite
pnpm dlx create-vite
通过附加的命令行选项直接指定项目名称和你想要使用的模板。例如要构建一个 Vite + Vue 项目,可运行一下命令:
- # npm 6.x
- npm init vite@latest my-vue-app --template vue
-
- # npm 7+, 需要额外的双横线:
- npm init vite@latest my-vue-app -- --template vue
-
- # yarn
- yarn create vite my-vue-app --template vue
-
npm install vue-router -S
router
目录,新建index.js
文件 - // index.js
- import { createRouter, createWebHashHistory } from 'vue-router'
- import App from '@/App.vue'
-
- // 路由规则
- const routes = [
- {
- path: '/',
- component: App,
- children: [
- { path: '', redirect: '/home' },
- {
- path: '/home',
- name: 'home',
- component: () => import('@/views/Home.vue')
- },
- {
- path: '/login',
- name: 'login',
- component: () => import('@/views/Login.vue')
- },
- {
- // vue-router@4的变化,舍弃*通配符
- // 官方文档:https://next.router.vuejs.org/zh/guide/migration/index.html#%E5%88%A0%E9%99%A4%E4%BA%86-%EF%BC%88%E6%98%9F%E6%A0%87%E6%88%96%E9%80%9A%E9%85%8D%E7%AC%A6%EF%BC%89%E8%B7%AF%E7%94%B1
- path: '/:pathMatch(.*)*',
- name: '404',
- component: () => import('@/views/404.vue')
- }
- ]
- }
- ]
-
- const router = createRouter({
- // vueRouter@3版本的mode改成了history,hash模式配置createWebHashHistory,history模式配置createWebHistory
- history: createWebHashHistory(),
- routes
- })
-
- export default router
-

main.js
中引入router - import router from '@/router'
-
- const app = createApp(App)
-
- // 路由
- app.use(router)
npm install axios -S
在utils/http
目录新建index.js
- import axios from 'axios'
-
- const service = axios.create({
- // baseURL: '',
- timeout: 30 * 1000,
- // 请求是否携带cookie
- withCredentials: true
- })
-
- // 请求拦截器
- // service.interceptors.request.use(config => {
-
- // }, err => {
-
- // })
-
- // 响应拦截器
- // service.interceptors.response.use(res => {
-
- // }, err => {
-
- // })
-
- export default service
-

npm i vant@next -S
具体可查看官方文档中的rem适配方案
npm i amfe-flexible -S
import 'amfe-flexible'
npm i postcss-pxtorem -D
vite.config.js
中加入以下配置 - // Vite自身已经集成PostCSS,无需再次安装。另外也无需单独创建PostCSS配置文件,已集成到vite.config.js的css选项中
- import postCssPxToRem from 'postcss-pxtorem'
-
- css: {
- postcss: {
- plugins: [
- postCssPxToRem({
- rootValue: 37.5,
- propList: ['*'],
- })
- ]
- }
- },
-
npm i less less-loader -D
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。