本文系原创,转载请附带作者信息。项目地址: https://github.com/momozjm/vant-project.git

前言

在项目开发过程中,一个新的项目需要我们从零开始搭建框架,这个时候我们就可以用网上很多的脚手架进行开发,但是我们在业务开发时,还需要对项目的架构进行完善。如果有一个类似于ant design pro这种类型的项目可以拿来即用,不需要过多的配置,就可以进行开发的话,岂不是美滋滋。

所以说为了便于后期的快速开发,我决定利用vue-cli+vant来打造一个移动端开发的模板,后期进行迭代,完善功能。

项目功能模块(不断更新中...)

项目代码

整个项目的脚手架是用vue-cli生成的,具体的生成方式可以自己网上查阅。下面我就简单介绍一下项目中添加了哪些功能模块

一、rem的适配

使用postcss-pxtoremflexable.js结合的形式,适配各类机型。此配置是以2倍屏的基础来配置的,也就是说你的设计图也是以2倍屏设计的,这样设计图上的px值是多少你就可以直接拷过来,省去了px转rem的换算。

1:postcss-pxtorem的配置:在vue.config.js中添加如下代码(需要自己新建vue.config.js文件)

  1. css: {
  2. loaderOptions: {
  3. postcss: {
  4. plugins: [
  5. require('postcss-pxtorem')({
  6. rootValue: 37.5, // 换算的基数
  7. propList: ['*'],
  8. }),
  9. ]
  10. }
  11. }
  12. }

2:flexable.js。文件路径src>utils>flexable.js(我一般把项目需要的公共方法或者配置放在utils文件夹下)

  1. !function (n) {
  2. var e = n.document,
  3. t = e.documentElement,
  4. i = 750,
  5. d = i / 100,
  6. o = "orientationchange" in n ? "orientationchange" : "resize",
  7. a = function () {
  8. var n = t.clientWidth || 320; n > 750 && (n = 750);
  9. t.style.fontSize = n / d + "px"
  10. };
  11. e.addEventListener && (n.addEventListener(o, a, !1), e.addEventListener("DOMContentLoaded", a, !1))
  12. }(window)

3:在main.js中引入flexable.js

  1. import '@/utils/flexable'

二、axios请求封装

1:request.js。文件路径src>utils>request.js

  1. import axios from 'axios'
  2. // 创建 axios 实例
  3. const service = axios.create({
  4. baseURL: process.env.VUE_APP_API_BASE_URL, // api base_url
  5. timeout: 6000 // 请求超时时间
  6. })
  7. const err = (error) => {
  8. if (error.response) {
  9. if (error.response.status !== 200) {
  10. console.log(error)
  11. return
  12. }
  13. }
  14. return Promise.reject(error)
  15. }
  16. // request interceptor
  17. service.interceptors.request.use(config => {
  18. // const token = 'token'
  19. // if (token) {
  20. // config.headers['Access-Token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  21. // }
  22. return config
  23. }, err)
  24. // response interceptor
  25. service.interceptors.response.use((response) => {
  26. return response.data
  27. }, err)
  28. export {
  29. service as axios
  30. }

三、vuex

在views下新建了一个About.vue。在此组件中走了一遍异步获取数据的流程(接口是乱写的,报404)。大致流程为:页面触发action ---> action中异步回去数据并commit一个mutations ---> mutations中修改state里的值 ---> 视图更新

1:About.vue

  1. <template>
  2. <div class="about">
  3. <Button type="primary" @click="getDetail" id="detail">点击调用接口获取详情</Button>
  4. <router-link to="/">Home</router-link>
  5. <router-link to="/scroll">Scroll</router-link>
  6. </div>
  7. </template>
  8. <script>
  9. import { mapState, mapActions } from "vuex";
  10. import { Button } from "vant";
  11. export default {
  12. computed: {
  13. ...mapState("about", ["detail"])
  14. },
  15. created() {
  16. // this.getDetail();
  17. },
  18. methods: {
  19. ...mapActions("about", ["getDetail"])
  20. },
  21. components: {
  22. Button
  23. }
  24. };
  25. </script>
  26. <style lang="less">
  27. .about {
  28. height: 100vh;
  29. font-size: 14px;
  30. }
  31. #detail {
  32. font-size: 14px;
  33. }
  34. </style>

2:index.js。文件文职@>store>index.js

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import about from './modules/about'
  4. // import scroll from './modules/scroll'
  5. Vue.use(Vuex)
  6. export default new Vuex.Store({
  7. modules: {
  8. about,
  9. // scroll
  10. },
  11. })

3:about.js。文件位置@>store>modules>about.js

  1. import { getDetailApi } from '@/api/api'
  2. const state = {
  3. detail: {}
  4. }
  5. const mutations = {
  6. setDetail: (state, data) => {
  7. state = {
  8. ...state,
  9. detail: data
  10. }
  11. }
  12. }
  13. const actions = {
  14. getDetail({ commit }) {
  15. getDetailApi().then(res => {
  16. commit('setDetail', res)
  17. })
  18. },
  19. }
  20. export default {
  21. namespaced: true,
  22. state,
  23. mutations,
  24. actions
  25. }

4:api.js。 文件位置@>api>api.js

  1. import { axios } from '@/utils/request'
  2. const api = {
  3. // 获取详情
  4. detail: '/detail'
  5. }
  6. export function getDetailApi(parameter) {
  7. console.log(parameter)
  8. return axios({
  9. url: api.detail,
  10. method: 'get'
  11. })
  12. }
  13. // export function getDetail() {
  14. // return axios({
  15. // url: api.detail,
  16. // method: 'post',
  17. // data: {}
  18. // })
  19. // }

四、Vant

新建一个页面添加了下拉刷新和上拉加载功能。后续会以组件的形式再封装一些常用的功能。

Vant没有在main.js里全局注册,而是使用动态引入的方式。根目录新建.babelrc

  1. {
  2. "plugins": [
  3. ["import", {
  4. "libraryName": "vant",
  5. "libraryDirectory": "es",
  6. "style": true
  7. }]
  8. ]
  9. }

五、webpack配置

vue-cli3以后生成的项目,修改webpack都要在项目根目录新建一个vue.config.js来修改你的配置

  1. module.exports = {
  2. lintOnSave: true,
  3. // 生产环境打包资源路径
  4. publicPath: '/',
  5. outputDir: "dist",
  6. assetsDir: "static",
  7. // postcss-pxtorem配置
  8. css: {
  9. loaderOptions: {
  10. postcss: {
  11. plugins: [
  12. require('postcss-pxtorem')({
  13. rootValue: 37.5, // 换算的基数
  14. propList: ['*'],
  15. }),
  16. ]
  17. }
  18. }
  19. },
  20. // 代理
  21. devServer: {
  22. // development server port 8000
  23. // port: 8000,
  24. // // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
  25. // proxy: {
  26. // "/api": {
  27. // // target: "http://xx.xx.xx.xx:xxxx/",
  28. // changeOrigin: true,
  29. // pathRewrite: {
  30. // '^/api': '/'
  31. // }
  32. // }
  33. // }
  34. },
  35. // 生产环境去掉sourceMap
  36. productionSourceMap: false,
  37. }

总结

这个框架只具备开发vue + vant的基本功能,总体上的目标算是达到了,后续也会迭代添加一些组件或者功能。整个过程中算是对自己架构能力进行一些锻炼。

感谢你的阅读,如有修改或者建议的地方,欢迎提出哦。

利用vue-cli + vant搭建一个移动端开发模板的更多相关文章

  1. [转]Vue CLI 3搭建vue+vuex 最全分析

    原文地址:https://my.oschina.net/wangnian/blog/2051369 一.介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统.有三个组件: CLI:@ ...

  2. Vue CLI 3搭建vue+vuex 最全分析

    一.介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统.有三个组件: CLI:@vue/cli 全局安装的 npm 包,提供了终端里的vue命令(如:vue create .vue ...

  3. 利用vue-cli配合vue-router搭建一个完整的spa流程

    好文章备忘录: 转自:https://segmentfault.com/a/1190000009160934?_ea=1849098 demo源码:https://github.com/1590123 ...

  4. VUE CLI环境搭建文档

    VUE CLI环境搭建文档 1.安装Node.js 下载地址 https://nodejs.org/zh-cn/download/ 2.全局安装VUE CLI win+R键打开运行cmd窗口输入一下代 ...

  5. 利用git+hugo+markdown 搭建一个静态网站

    利用git+hugo+markdown 搭建一个静态网站 一直想要有一个自己的文档管理系统: 可以很方便书写,而且相应的文档很容易被分享 很方便的存储.管理.历史记录 比较方面的浏览和查询 第一点用M ...

  6. 利用Wamp在本地搭建一个wordpress站点

    原文链接:利用Wamp在本地搭建一个wordpress站点 有时候我们会想搭建一个自己的站点,可是由于只是想自己访问,就不是很想为这个站点在买一个服务器和域名,那我们可能首先就想到把自己电脑当做服务器 ...

  7. 使用Vue CLI脚手架搭建vue项目

    本次是使用@vue/cli 3.11.0版本搭建的vue项目 1. 首先确保自己的电脑上的Node.js的版本是8.9版本或者以上 2. 全局安装vue/cli npm install @vue/cl ...

  8. 技术人如何利用 github+Jekyll ,搭建一个独立免费的技术博客

    上次有人留言说,技术博客是程序员的标配,但据我所知绝大部分技术同学到现在仍然没有自己的技术博客.原因有很多,有的是懒的写,有的是怕写不好,还有的是一直想憋个大招,幻想做到完美再发出来,结果一直胎死腹中 ...

  9. 如何使用Vue.js来搭建一个后台管理系统

    目录 使用的技术 基础但不好版 1.初始化项目 2.实现初始页内容自定义 3.使用路由 原始代码 自建页面 修改路由 4.测试路由跳转 补充 子路由版 嵌套router-view 定义子路由 修改菜单 ...

随机推荐

  1. 收集免费的接口服务,做一个api的搬运工

    hello, 大家好,今天给大家推荐的开源项目在某种程度上极大的方便了广大的开发者,这个开源项目统计了网上诸多的免费API,为广大开发者收集免费的接口服务,专心致志做一个API的搬运工,每月定时更新新 ...

  2. IOCP完成端口

    转:https://blog.csdn.net/piggyxp/article/details/6922277 本系列里完成端口的代码在两年前就已经写好了,但是由于许久没有写东西了,不知该如何提笔,所 ...

  3. Daily Scrum 1/11/2016

    Zhaoyang & Minlong: Took and edited the video which introduced our APP. Yandong: Summarized bugs ...

  4. Daily Scrum 1/4/2015

    Process: After New year's Day, we start our project in plan. Zhanyang: Add some useful UI in the IOS ...

  5. Springboot:静态资源加载(七)

    WebMvc自动配置: 搜索WebMvcAutoConfiguration自动装配类: 第一种方式通过webjars加载静态资源: https://www.webjars.org(通过maven加载依 ...

  6. Python(5)

    把 aaabbcccd 这种形式的字符串压缩成 a3b2c3d1 这种形式. print(''.join({i+str(s.count(i)) for i in s})) dic={} for i i ...

  7. (第七篇)系统编码、自启动配置、HOSTNAME、系统启动、定时任务、进程管理、硬盘及其分区

    linux查看系统编码和修改系统编码的方法 查看支持的字符编码 使用locale命令, 如: root@ubuntu:/etc# locale 然后修改/etc/locale.conf,如改成中文编码 ...

  8. 徐州赛区网络预赛 D Easy Math

    比赛快结束的适合看了一下D题,发现跟前几天刚刚做过的HDU 5728 PowMod几乎一模一样,当时特兴奋,结果一直到比赛结束都一直WA.回来仔细一琢磨才发现,PowMod这道题保证了n不含平方因子, ...

  9. 开发AI+诊疗生发系统,「先锋汇美」借力人工智能辅助诊疗实现头皮医学检测...

    困扰年轻人的脱发问题萌生了新兴的产业链.36氪先前曾剖析过近来火热的植发市场,更多人则选择"防范于未然","头皮检测"服务备受关注.此前,人们对"头皮 ...

  10. vue elementui table 双击单元格实现编辑,聚焦,失去焦点,显示隐藏input和span

    <el-table :data="tableData" class="tb-edit" style="width: 100%" ref ...