使用Vue全家桶开发移动端页面。

本博文默认已安装node.js。

github链接

一.准备工作

  • 安装vue
npm install vue
  • 安装脚手架vue-cli
npm install -g @vue/cli
  • 创建webpack项目
vue init webpack my-app

  • 运行
cd my-app
npm run dev



按照提示,在浏览器打开http://localhost:8082/,效果如下:

  • 安装状态管理vuex
npm install vuex --save-dev
  • 目录结构

    项目初始目录如下:

至此,准备工作已就绪,接下来将进行项目整体的结构设计。

二.项目设计

1.项目目录设计

  • assets目录下创建imagesjscss文件夹。

    • images文件夹下创建index文件夹,用于存放 首页图片(模块化,让项目结构一目了然)。

  • src目录下创建pages目录,用户存放不同的功能页面。

    • 再在pages目录下创建index首页目录。

    • index目录下创建index.vue主文件。

  • 修改router

    • 修改index.js
    import Vue from 'vue'
    import Router from 'vue-router'
    import Index from '@/pages/index' Vue.use(Router) export default new Router({
    routes: [
    {
    path: '/',
    name: 'Index',
    component: Index
    }
    ]
    })
    • 浏览器效果

  • 删除components目录下的文件,将此目录作为页面组件文件夹,在此目录下创建index文件夹,存放index首页的组件。

  • pages/index/index.vue中引入header组件

<template>
<div class="index-wrap">
<comHeader />
你好,首页
</div>
</template> <script>
import header from '@/components/index/header' export default {
data() {
return { }
},
components: {
comHeader: header
}
} </script> <style scoped> </style>

至此,项目的整个结构被重新设计完成,接下来让我们引入rem.jsless来让我们开发起来更舒服。

2.移动端适配和less编译,让开发变得快乐起来。

  • 移动端适配 rem

    assets/js文件夹下创建common文件夹存放公共js,再在common文件夹下创建rem适配文件js。并在main.js中引入。
// rem.js
;(function (designWidth, maxWidth) {
var doc = document,
win = window;
var docEl = doc.documentElement;
var tid;
var rootItem, rootStyle; function refreshRem() {
var width = docEl.getBoundingClientRect().width;
if (!maxWidth) {
maxWidth = 540;
}
;
if (width > maxWidth) {
width = maxWidth;
}
//与淘宝做法不同,直接采用简单的rem换算方法1rem=100px
var rem = width * 100 / designWidth;
//兼容UC开始
rootStyle = "html{font-size:" + rem + 'px !important}';
rootItem = document.getElementById('rootsize') || document.createElement("style");
if (!document.getElementById('rootsize')) {
document.getElementsByTagName("head")[0].appendChild(rootItem);
rootItem.id = 'rootsize';
}
if (rootItem.styleSheet) {
rootItem.styleSheet.disabled || (rootItem.styleSheet.cssText = rootStyle)
} else {
try {
rootItem.innerHTML = rootStyle
} catch (f) {
rootItem.innerText = rootStyle
}
}
//兼容UC结束
docEl.style.fontSize = rem + "px";
};
refreshRem(); win.addEventListener("resize", function () {
clearTimeout(tid); //防止执行两次
tid = setTimeout(refreshRem, 300);
}, false); win.addEventListener("pageshow", function (e) {
if (e.persisted) { // 浏览器后退的时候重新计算
clearTimeout(tid);
tid = setTimeout(refreshRem, 300);
}
}, false);
if (doc.readyState === "complete") { doc.body.style.fontSize = "16px";
} else {
doc.addEventListener("DOMContentLoaded", function (e) {
doc.body.style.fontSize = "16px";
}, false);
}
})(360, 750); // 360为设计图的尺寸,请按照实际设计图修改 // main.js
import Vue from 'vue'
import App from './App'
import router from './router'
import '@/assets/js/common/rem.js' // 引入rem.js Vue.config.productionTip = false new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})

至此,rem适配已完成,在写style时便可直接按照 (设计图尺寸 / 100) rem,例如设计图给到元素height为200px,那么用rem则为height: 2rem;

  • less 编译,在写style时,为了高效开发,我们选用less编译。
// 安装less和依赖
npm install less less-loader style-loader --save-dev

以header.vue为例

<template>
<div class="header-wrap">
我是头头头
<div class="title">
title
</div>
</div>
</template> <script> </script> <style scoped lang="less">
.header-wrap{
height: 1rem;
background-color: #252627;
.title{
color: #fff;
height: .5rem;
}
}
</style>

至此就可以开始页面的开发了。

3.状态管理store

  • 安装 vuex
npm install vuex --save
  • 创建store目录结构,至于store原理望大家自已学习掌握。

    • index.js
    /**
    * 组装模块并导出 store
    */ import Vue from 'vue'
    import Vuex from 'vuex'
    import game from './modules/game'
    import * as actions from './actions'
    import mutations from './mutations'
    import getters from './getters' Vue.use(Vuex) const debug = process.env.NODE_ENV !== 'production' const state = {
    userInfo:{}
    } export default new Vuex.Store({
    state: state,
    actions: actions,
    mutations: mutations,
    getters:getters,
    modules: {
    game,
    },
    strict: debug
    })
  • 使用store

this.$store.dispatch('getData', response.data.data)

4.数据请求 axios

  • src目录下穿件utils目录用于存放工具js。在utils下创建request.jsaxios请求进行封装。
import axios from 'axios'
import { Message, MessageBox } from 'element-ui'
import config from '../../config/config'
import toast from './toast' import store from '@/store' const service = axios.create({
baseURL: 'www.baidu.com',
timeout:0// request timeout
})
service.interceptors.request.use(
requestConfig => {
let data = {
// 公共请求参数
};
requestConfig.data = Object.assign({}, requestConfig.data, data)
return requestConfig
},
error => {
Promise.reject(error)
}
)
// response interceptor
service.interceptors.response.use(response => {
const res = response.data
if (res.errno === 501) {
MessageBox.alert('系统未登录,请重新登录', '错误', {
confirmButtonText: '确定',
type: 'error'
}).then(() => {
})
return Promise.reject('error')
} else if (res.errno === 502) {
toast.showToast('系统内部错误,请联系管理员维护',1200,'error')
return Promise.reject('error')
} else if (res.errno === 503) {
toast.showToast('请求业务目前未支持',1200,'error')
return Promise.reject('error')
} else if (res.errno === 504) {
toast.showToast('更新数据已经失效,请刷新页面重新操作',1200,'error')
return Promise.reject('error')
} else if (res.errno === 505) {
toast.showToast('更新失败,请再尝试一次',1200,'error')
return Promise.reject('error')
} else if (res.errno === 506) {
toast.showToast('没有操作权限,请联系管理员授权',1200,'error')
return Promise.reject('error')
} else {
return response
}
}, error => {
toast.showToast('登录连接超时',5 * 1000,'error')
return Promise.reject(error)
})
export default service
  • src 目录下常见 api 文件夹,并创建组件index.js
import request from '../utils/request'

/**
* @method getUserInfo 获取用户信息
* @param query {Object}
*/
export function getUserInfo(query){
return request({
url: 'user/info',
method: 'post',
data: query
})
}
  • index.vue中调用
<template>
<div class="index-wrap">
<comHeader />
你好,首页
</div>
</template> <script>
import header from '@/components/index/header'
import { getUserInfo } from '@/api/index.js' // 引入 export default {
data() {
return { }
},
components: {
comHeader: header
},
methods: {
getInfo(){
getUserInfo() //业务逻辑
.then(res => {
// do something
})
.catch(response => {})
}
},
created() {
this.getInfo(); //调用
}
} </script> <style scoped lang="less"> </style>

三.编译发布

1.编译生成 dist

npm run build

//在根目录下生成dis文件夹,可以将此文件夹放到oss上以供不同浏览器浏览。

总结

  • 在开发项目的过程中要考虑项目的模块化。
  • 尽可能的做到代码规范,具体的代码规范可在我的其他博文参考下:
  • 实际项目中的具体问题可私信我,共同解决共同学习。为你能看完本博文而感到愉悦

【实战】Vue全家桶(vue + axios + vue-router + vuex)搭建移动端H5项目的更多相关文章

  1. vue全家桶+Koa2开发笔记(1)--vuex

    1.  安装webpack的问题: webpack坑系列--安装webpack-cli 2.  vue-cli(vue脚手架)超详细教程 3.  在命令行中使用 touch 执行新建文件: 4.  关 ...

  2. Vue 全家桶,深入Vue 的世界

    内容简介: Vue 实例上的属性 Vue 生命周期 Vue 数据绑定 computed 计算属性 watch 监听器 Vue 组件 Vue 组件 extend Vue 组件高级属性 Vue 的rend ...

  3. vue全家桶(vue-cli,vue-router,vue-resource,vuex)-1

    vue-cli # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新项目 $ vue init webpack my- ...

  4. 使用vue全家桶制作博客网站

    前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用vue全家桶制作的博客网站 概述 该项目是基于vue全家桶(vue.vue-router.vuex.v ...

  5. 转载: 使用vue全家桶制作博客网站 HTML5 移动网站制作的好教程

    使用vue全家桶制作博客网站   前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用vue全家桶制作的博客网站 概述 该项目是基于vue全家桶(vue. ...

  6. vue全家桶+axios+jsonp+es6 仿肤君试用小程序

    vue全家桶+axios+jsonp+es6 仿肤君试用小程序 把自己写的一个小程序项目用vue来实现的,代码里面有一些注释,主要使用了vue-cli,vue,vuex,vue-router,axoi ...

  7. 用 Vue 全家桶二次开发 V2EX 社区

    一.开发背景 为了全面的熟悉Vue+Vue-router+Vuex+axios技术栈,结合V2EX的开放API开发了这个简洁版的V2EX. 在线预览 (为了实现跨域,直接npm run dev部署的, ...

  8. Vue全家桶介绍

    一直不清楚全家桶是什么玩意,上网搜了一下,才知道就是平时项目中使用的几个依赖包,下面分享一下 Vue 全家桶介绍 Vue有著名的全家桶系列,包含了vue-router(http://router.vu ...

  9. 升级vue全家桶过程记录

    背景 如果你使用了element-ui的el-tabs组件,并且想要单独升级element-ui至2.10.0,你会发现,使用了el-tabs组件的页面只要打开就卡死.原因是element-ui~2. ...

随机推荐

  1. Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)

    使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...

  2. Java 之 反射机制

    反射:框架设计的灵魂 框架:是一个可以供我们使用的半成品软件.可以在框架的基础上进行软件开发,简化编码. 反射:将类的各个组成部分封装为其他对象,这就是反射机制. 好处: 1. 可以在程序运行过程中, ...

  3. iOS-CGContextRef

    图形上下文(Graphics Context)---绘制目标 需要在iOS应用程序的屏幕上进行绘制时,需要先定义一个UIView类,并实现它的drawRect:方法,当启动程序时,会先调用loadVi ...

  4. 利用 CAKeyframeAnimation实现任意轨迹移动

      自定义 View,实现以下方法即可 - (void)drawRect:(CGRect)rect { // Drawing code // 初始化UIBezierPath UIBezierPath ...

  5. Vue注意事项

    在使用Vue中的函数或自己定义的函数或指令的时候,Vue说明如下 在一些自己定义或系统定义的驼峰命名规则的时候,你需要到元素区域引用的使用中间的大写要改成小写在谭家 一条横杠如: 你在var=new ...

  6. Zabbix MySQL percona 模板部署

    Zabbix MySQL percona服务端执行以下操作https://www.zabbix.com/download?zabbix=4.0&os_distribution=centos&a ...

  7. Linux命令——procinfo

    简介 proc文件系统是一个虚拟文件系统,包含有关进程和系统信息的文件. proc 文件系统开机时自动挂载并映射到/proc目录.许多程序从/proc目录中检索信息,对其进行处理并使其易于用于各种目的 ...

  8. 关于MySQL中的锁机制详解

    锁概述 MySQL的锁机制,就是数据库为了保证数据的一致性而设计的面对并发场景的一种规则. 最显著的特点是不同的存储引擎支持不同的锁机制,InnoDB支持行锁和表锁,MyISAM支持表锁. 表锁就是把 ...

  9. JIT优化的小问题

    同事问了个问题,挺有意思的,代码: public class TestJIT{ private static boolean sss; public static void main(String[] ...

  10. jFinal的小知识点总结

    sql批处理 // 批处理sql List<String> sqlList = new ArrayList<String>(); sqlList.add("delet ...