项目准备

  • 通过 Vue CLI 创建项目

    1. vue create lagou-deu-mobile
  • 选项配置

    1. ? Please pick a preset: Manually select features
    2. ? Check the features needed for your project: Babel, Router, Vuex, CSS Pre-processors, Linter
    3. ? Use history mode for router? (Requires proper server setup for index fallback in production) No
    4. ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with dart-sass)
    5. ? Pick a linter / formatter config: Standard
    6. ? Pick additional lint features: Lint on save, Lint and fix on commit
    7. ? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated config files
    8. ? Save this as a preset for future projects? No
  • 进入项目并运行

    1. cd lagou-deu-mobile
    2. npm run serve
  • 项目基础结构

    1. lagou-deu-mobile
    2. ├─ .browserslistrc
    3. ├─ .editorconfig
    4. ├─ .eslintrc.js
    5. ├─ .gitignore
    6. ├─ README.md
    7. ├─ babel.config.js
    8. ├─ package-lock.json
    9. ├─ package.json
    10. ├─ public
    11. ├─ favicon.ico
    12. └─ index.html
    13. └─ src
    14. ├─ App.vue
    15. ├─ assets ------------------------------- 静态资源文件
    16. ├─ components --------------------------- 公共组件
    17. ├─ main.js ------------------------------ 入口文件
    18. ├─ router ------------------------------- 路由
    19. └─ index.js
    20. ├─ services ----------------------------- 请求接口
    21. ├─ store -------------------------------- 状态管理文件
    22. └─ index.js
    23. ├─ utils -------------------------------- 工具函数
    24. └─ views -------------------------------- 页面文件

Vant 组件库

文档网站

安装

  1. # Vue 2 项目,安装 Vant 2:
  2. npm i vant -S
  3. # Vue 3 项目,安装 Vant 3:
  4. npm i vant@next -S

通过脚手架安装

  1. # 安装 Vue Cli
  2. npm install -g @vue/cli
  3. # 创建一个项目
  4. vue create hello-world
  5. # 创建完成后,可以通过命令打开图形化界面,如下图所示
  6. vue ui

引入组件

  • src/main.js ```javascript import Vant from ‘vant’ import ‘vant/lib/index.css’

Vue.use(Vant)

  1. <a name="T6HCz"></a>
  2. ## 浏览器适配
  3. <a name="pMd6t"></a>
  4. ### 动态设置 rem 基准值
  5. [lib-flexible](https://github.com/amfe/lib-flexible/)
  6. - 安装
  7. ```powershell
  8. npm i -S amfe-flexible
  • 引入(需要作为第一个插件来引用) ```javascript // src/main.js

// 引入 lib-flexible import ‘amfe-flexible’

  1. <a name="UHuyO"></a>
  2. ### 自动将 px 转换为 rem
  3. - [postcss-pxtorem](https://github.com/cuth/postcss-pxtorem)
  4. - [postcss中文文档](https://github.com/postcss/postcss/blob/main/docs/README-cn.md)
  5. - 安装
  6. ```powershell
  7. npm install postcss-pxtorem@5.1.1 --save-dev
  • 配置
    1. // .postcssrc
    2. module.exports = {
    3. plugins: {
    4. // 自动添加 css 浏览器前缀
    5. 'autoprefixer': {
    6. // 浏览器信息配置 在 .browserslistrc 中进行配置
    7. // browsers: ['Android >= 4.0', 'iOS >= 8']
    8. },
    9. // px/rem 单位转换
    10. 'postcss-pxtorem': {
    11. rootValue: 37.5,//默认值
    12. propList: ['*']//被处理的属性列表
    13. }
    14. }
    15. }

路由处理

  • 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

  1. <a name="dt0Qr"></a>
  2. ## 封装请求模块
  3. - [接口文档](http://eduboss.lagou.com/front/doc.html#/home)
  4. - `src/utile/requres.js`
  5. ```javascript
  6. import axios from 'axios'
  7. const request = axios.create({
  8. baseURL: 'http://edufront.lagou.com'
  9. })
  10. export default request

公共组件-导航

  • src/components/LayoutFooter.vue
    1. <template>
    2. <div class="layout-fotter">
    3. <van-tabbar router>
    4. <van-tabbar-item replace to='/' icon='orders-o'>选课</van-tabbar-item>
    5. <van-tabbar-item replace to='/learn' icon='desktop-o'>学习</van-tabbar-item>
    6. <van-tabbar-item replace to='/user' icon='user-o'></van-tabbar-item>
    7. </van-tabbar>
    8. </div>
    9. </template>
    10. <script>
    11. export default {
    12. name: 'LayoutFooter'
    13. }
    14. </script>
    15. <style lang="scss" scoped>
    16. </style>