【实战】Vue全家桶(vue + axios + vue-router + vuex)搭建移动端H5项目
使用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
目录下创建images
、js
、css
文件夹。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.js
和less
来让我们开发起来更舒服。
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.js
对axios
请求进行封装。
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项目的更多相关文章
- vue全家桶+Koa2开发笔记(1)--vuex
1. 安装webpack的问题: webpack坑系列--安装webpack-cli 2. vue-cli(vue脚手架)超详细教程 3. 在命令行中使用 touch 执行新建文件: 4. 关 ...
- Vue 全家桶,深入Vue 的世界
内容简介: Vue 实例上的属性 Vue 生命周期 Vue 数据绑定 computed 计算属性 watch 监听器 Vue 组件 Vue 组件 extend Vue 组件高级属性 Vue 的rend ...
- vue全家桶(vue-cli,vue-router,vue-resource,vuex)-1
vue-cli # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新项目 $ vue init webpack my- ...
- 使用vue全家桶制作博客网站
前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用vue全家桶制作的博客网站 概述 该项目是基于vue全家桶(vue.vue-router.vuex.v ...
- 转载: 使用vue全家桶制作博客网站 HTML5 移动网站制作的好教程
使用vue全家桶制作博客网站 前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用vue全家桶制作的博客网站 概述 该项目是基于vue全家桶(vue. ...
- vue全家桶+axios+jsonp+es6 仿肤君试用小程序
vue全家桶+axios+jsonp+es6 仿肤君试用小程序 把自己写的一个小程序项目用vue来实现的,代码里面有一些注释,主要使用了vue-cli,vue,vuex,vue-router,axoi ...
- 用 Vue 全家桶二次开发 V2EX 社区
一.开发背景 为了全面的熟悉Vue+Vue-router+Vuex+axios技术栈,结合V2EX的开放API开发了这个简洁版的V2EX. 在线预览 (为了实现跨域,直接npm run dev部署的, ...
- Vue全家桶介绍
一直不清楚全家桶是什么玩意,上网搜了一下,才知道就是平时项目中使用的几个依赖包,下面分享一下 Vue 全家桶介绍 Vue有著名的全家桶系列,包含了vue-router(http://router.vu ...
- 升级vue全家桶过程记录
背景 如果你使用了element-ui的el-tabs组件,并且想要单独升级element-ui至2.10.0,你会发现,使用了el-tabs组件的页面只要打开就卡死.原因是element-ui~2. ...
随机推荐
- VS.NET(C#)--1.4项目与解决方案
项目与解决方案 项目 除创建网站,VS2005可创建项目.然后把项目放入解决方案中.VS2005可编译很多类型项目,分别是: 1.Windows应用程序 --在用戶计算机上运行的客户端应用程序,可显示 ...
- 【转载】 腾讯云通过设置安全组禁止某些IP访问你的服务器
有时候我们在运维网站的过程中会发现一些漏洞扫描者的IP信息,或者个人爬虫网站的IP信息,此时我们想禁止掉这些IP访问到你的服务器,可以通过腾讯云的安全组功能来设置禁止这些IP访问你的服务器,也可以通过 ...
- echarts字体适配
var html = document.getElementsByTagName("html")[0]; var width = html.clientWidth; var too ...
- FICO-财务凭证验证及替代
转载:https://wenku.baidu.com/view/9e2dae57d15abe23492f4d39.html?sxts=1561613818537 https://wenku.baidu ...
- Python 使用gevent下载图片案例
import urllib.request import gevent from gevent import monkey monkey.patch_all() def downloader(img_ ...
- 如何通过wlst部署应用程序到weblogic12c上
适用版本 Oracle WebLogic Server - Version 10.3 and laterInformation in this document applies to any plat ...
- python实用库
参考:https://github.com/programthink/opensource/blob/master/libs/python.wiki#35_ Python 开源库及示例代码 Table ...
- python返回值的缺省设置
有时候并不需要返回所有的值,但是原始函数的return语句中又有较多参数时: 方法一:修改原始返回值,只返回需要的参数 方法二:如果原始函数时第三方库或者python自带库,则直接修改可能不太好,于是 ...
- PS批量制作获奖证书并导出PNG
其实方法和"使用PS批量制作视频字幕"的方法类似.区别在于制作视频字幕时导出成psd格式就可以直接导入Premiere中使用了,而获奖证书考虑到打印设备有无PS的不确定性,可能需要 ...
- JavaScript中对数组的排序
将下列对象数组,通过工资属性,由高到低排序 var BaiduUsers = [], WechatUsers = []; var User = function(id, name, phone, ge ...