项目准备
通过 Vue CLI 创建项目
vue create lagou-deu-mobile
选项配置
? Please pick a preset: Manually select features? Check the features needed for your project: Babel, Router, Vuex, CSS Pre-processors, Linter? Use history mode for router? (Requires proper server setup for index fallback in production) No? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with dart-sass)? Pick a linter / formatter config: Standard? Pick additional lint features: Lint on save, Lint and fix on commit? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated config files? Save this as a preset for future projects? No
进入项目并运行
cd lagou-deu-mobilenpm run serve
项目基础结构
lagou-deu-mobile├─ .browserslistrc├─ .editorconfig├─ .eslintrc.js├─ .gitignore├─ README.md├─ babel.config.js├─ package-lock.json├─ package.json├─ public│ ├─ favicon.ico│ └─ index.html└─ src├─ App.vue├─ assets ------------------------------- 静态资源文件├─ components --------------------------- 公共组件├─ main.js ------------------------------ 入口文件├─ router ------------------------------- 路由│ └─ index.js├─ services ----------------------------- 请求接口├─ store -------------------------------- 状态管理文件│ └─ index.js├─ utils -------------------------------- 工具函数└─ views -------------------------------- 页面文件
Vant 组件库
安装
# Vue 2 项目,安装 Vant 2:npm i vant -S# Vue 3 项目,安装 Vant 3:npm i vant@next -S
通过脚手架安装
# 安装 Vue Clinpm install -g @vue/cli# 创建一个项目vue create hello-world# 创建完成后,可以通过命令打开图形化界面,如下图所示vue ui
引入组件
src/main.js```javascript import Vant from ‘vant’ import ‘vant/lib/index.css’
Vue.use(Vant)
<a name="T6HCz"></a>## 浏览器适配<a name="pMd6t"></a>### 动态设置 rem 基准值[lib-flexible](https://github.com/amfe/lib-flexible/)- 安装```powershellnpm i -S amfe-flexible
- 引入(需要作为第一个插件来引用) ```javascript // src/main.js
// 引入 lib-flexible import ‘amfe-flexible’
<a name="UHuyO"></a>### 自动将 px 转换为 rem- [postcss-pxtorem](https://github.com/cuth/postcss-pxtorem)- [postcss中文文档](https://github.com/postcss/postcss/blob/main/docs/README-cn.md)- 安装```powershellnpm install postcss-pxtorem@5.1.1 --save-dev
- 配置
// .postcssrcmodule.exports = {plugins: {// 自动添加 css 浏览器前缀'autoprefixer': {// 浏览器信息配置 在 .browserslistrc 中进行配置// browsers: ['Android >= 4.0', 'iOS >= 8']},// px/rem 单位转换'postcss-pxtorem': {rootValue: 37.5,//默认值propList: ['*']//被处理的属性列表}}}
路由处理
src/router/index.js```javascript import Vue from ‘vue’ import VueRouter from ‘vue-router’ import Course from ‘@/views/course’ Vue.use(VueRouter)
const routes = [ // 登陆页 { path: ‘/login’, name: ‘login’, component: () => import(/ webpackChunkName: ‘login’ / ‘@/views/login’) }, // 首页 { path: ‘/‘, name: ‘course’, component: Course }, // 学习页面 { path: ‘/learn’, name: ‘learn’, component: () => import(/ webpackChunkName: ‘learn’ / ‘@/views/learn’), meta: { requiresAuth: true } }, // 用户页 { path: ‘/user’, name: ‘user’, component: () => import(/ webpackChunkName: ‘login’ / ‘@/views/user’) }, // 404 { path: ‘/‘, name: ‘error-page’, component: () => import(/ webpackChunkName: ‘error-page’ */ ‘@/views/error-page’) } ]
const router = new VueRouter({ routes })
export default router
<a name="dt0Qr"></a>## 封装请求模块- [接口文档](http://eduboss.lagou.com/front/doc.html#/home)- `src/utile/requres.js````javascriptimport axios from 'axios'const request = axios.create({baseURL: 'http://edufront.lagou.com'})export default request
公共组件-导航
src/components/LayoutFooter.vue<template><div class="layout-fotter"><van-tabbar router><van-tabbar-item replace to='/' icon='orders-o'>选课</van-tabbar-item><van-tabbar-item replace to='/learn' icon='desktop-o'>学习</van-tabbar-item><van-tabbar-item replace to='/user' icon='user-o'>我</van-tabbar-item></van-tabbar></div></template><script>export default {name: 'LayoutFooter'}</script><style lang="scss" scoped></style>
