已配置好的vue全家桶项目router,vuex,api,axios,vue-ls,async/await,less下载即使用
github 地址: https://github.com/liangfengbo/vue-cli-project 点击进入
vue-cli-project
- 已构建配置好的vuejs全家桶项目,统一管理后端接口 | 获取数据 | 请求数据,已包含vue-router,vuex,api,axios. webpack, 储存用vue-ls, 异步async/await, css less. 下载即使用项目开发。
- 喜欢或对你有帮助的话请点star✨✨,Thanks.
A Vue.js project
使用
# 安装服务
npm install
# 启动服务
npm run dev
说明
src架构
├── App.vue
├── api
│ ├── doctor.js
│ └── fetch.js
├── assets
│ └── logo.png
├── components
│ └── HelloWorld.vue
├── libs
│ └── util.js
├── main.js
├── router
│ └── index.js
├── store
│ ├── index.js
│ ├── modules
│ └── mutation-types.js
└── views
└── doctor
处理数据页面这2大块,把数据和页面分离,在vuex里面做请求数据,页面只需要做调用数据。
请求接口这块再细分,接口和请求接口分开,我使用了最新的async/await函数做数据请求
api文件夹 主要放后端提供的接口,如
import fetch from './fetch';
export default {
// 获取医生列表
list(params) {
return fetch.get('/doctor/list', params)
},
// 获取医生详情信息
detail(id) {
return fetch.post('/doctor/detail/' + id);
},
}
fetch.js 文件是封装axios请求,以及请求处理,和http状态码的等处理
import Util from '../libs/util'
import qs from 'qs'
import Vue from 'vue'
Util.ajax.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest'
};
Util.ajax.interceptors.request.use(config => {
/**
* 在这里做loading ...
* @type {string}
*/
// 获取token
config.headers.common['Authorization'] = 'Bearer ' + Vue.ls.get("web-token");
return config
}, error => {
return Promise.reject(error)
});
Util.ajax.interceptors.response.use(response => {
/**
* 在这里做loading 关闭
*/
// 如果后端有新的token则重新缓存
let newToken = response.headers['new-token'];
if (newToken) {
Vue.ls.set("web-token", newToken);
}
return response;
}, error => {
let response = error.response;
if (response.status == 401) {
// 处理401错误
} else if (response.status == 403) {
// 处理403错误
} else if (response.status == 412) {
// 处理412错误
} else if (response.status == 413) {
// 处理413权限不足
}
return Promise.reject(response)
});
export default {
post(url, data) {
return Util.ajax({
method: 'post',
url: url,
data: qs.stringify(data),
timeout: 30000,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
})
},
get(url, params) {
return Util.ajax({
method: 'get',
url: url,
params,
})
},
delete(url, params) {
return Util.ajax({
method: 'delete',
url: url,
params
})
},
put(url, data) {
return Util.ajax({
method: 'put',
url: url,
data: qs.stringify(data),
timeout: 30000,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
})
}
}
在vuex里面做请求,比如请求医生列表,用async/await,拿到数据存进store数据里面
├── index.js
├── modules
│ └── doctor.js
└── mutation-types.js
import doctor from '../../api/doctor'
import * as types from '../mutation-types'
const state = {
// 医生列表
doctorList: [],
// 医生详情信息
doctorDetail: null,
};
const mutations = {
// 设置医生列表
[types.SET_DOCTOR_LIST](state, data) {
state.doctorList = data
},
// 设置医生详情信息
[types.SET_DOCTOR_DETAIL](state, data) {
state.doctorDetail = data
},
};
const actions = {
/**
* 获取医生顾问列表
* @param state
* @param commit
* @param params
* @returns {Promise<void>}
*/
async getDoctorList({state, commit}, params) {
let ret = await doctor.list(params);
commit(types.SET_DOCTOR_LIST, ret.data.data);
},
/**
* 获取医生详情信息
* @param state
* @param commit
* @param id 医生ID
* @returns {Promise<void>}
*/
async getDoctorDetail({state, commit}, id) {
let ret = await doctor.detail(id);
commit(types.SET_DOCTOR_DETAIL, ret.data.data);
}
};
export default {
state,
actions,
mutations
}
在页面里需要执行引入:
import {mapActions, mapState} from 'vuex'
代码可以具体看文件的代码
└── doctor
├── detail.vue
└── list.vue
<script>
import {mapActions, mapState} from 'vuex'
export default {
components: {},
data() {
return {}
},
computed: {
...mapState({
doctorList: state => state.doctor.doctorList,
})
},
async created() {
// 医生类型
let params = {type: 'EXP'};
// 获取医生列表
await this.getDoctorList(params);
},
methods: {
...mapActions([
// 获取医生列表
'getDoctorList'
]),
// 路由跳转方法
routeLink(link) {
this.$router.push({path: link});
},
}
}
</script>
核心就是把数据和页面分离,细分把接口,请求数据用vuex做处理,在页面获取数据都做统一管理项目。可以具体看项目里面的代码。
github 地址: https://github.com/liangfengbo/vue-cli-project 点击进入
已配置好的vue全家桶项目router,vuex,api,axios,vue-ls,async/await,less下载即使用的更多相关文章
- vue全家桶项目搭建(vue-cli 2.9.6+vue-router+vuex+axios)
一.安装vue-cli + vue-router + vuex + axios 1.安装vue-cli 2.创建项目 3.安装vuex和axios 二.搭建项目目录结构,如下所示: 1.assets目 ...
- vue全家桶项目应用断断续续的记录
一.引用axios插件报错 axios使用文档 Cannot read property 'protocol' of undefined 解决方法:在mainjs文件中把axios引入vue的原型函数 ...
- vue全家桶(vue2.x+vue-router+axios+webpack)项目搭建
参考博客文章 博主FungLeo的Vue2+VueRouter2+Webpack+Axios 构建项目实战2017重制版 注:原博主写的非常详细 本文章为根据原博主教程总结的自己的搭建流程 一.安装n ...
- Vue 全家桶 + Electron 开发的一个跨三端的应用
代码地址如下:http://www.demodashi.com/demo/11738.html GitHub Repo:vue-objccn Follow: halfrost · GitHub 利用 ...
- 【实战】Vue全家桶(vue + axios + vue-router + vuex)搭建移动端H5项目
使用Vue全家桶开发移动端页面. 本博文默认已安装node.js. github链接 一.准备工作 安装vue npm install vue 安装脚手架vue-cli npm install -g ...
- 开箱即用 yyg-cli(脚手架工具):快速创建 vue3 组件库和vue3 全家桶项目
1 yyg-cli 是什么 yyg-cli 是优雅哥开发的快速创建 vue3 项目的脚手架.在 npm 上发布了两个月,11月1日进行了大升级,发布 1.1.0 版本:支持创建 vue3 全家桶项目和 ...
- 用 Vue 全家桶二次开发 V2EX 社区
一.开发背景 为了全面的熟悉Vue+Vue-router+Vuex+axios技术栈,结合V2EX的开放API开发了这个简洁版的V2EX. 在线预览 (为了实现跨域,直接npm run dev部署的, ...
- 使用vue全家桶制作博客网站
前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用vue全家桶制作的博客网站 概述 该项目是基于vue全家桶(vue.vue-router.vuex.v ...
- 转载: 使用vue全家桶制作博客网站 HTML5 移动网站制作的好教程
使用vue全家桶制作博客网站 前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用vue全家桶制作的博客网站 概述 该项目是基于vue全家桶(vue. ...
随机推荐
- SDCC2013大会笔记整理
2013-8-30 大会首日 百度移动云三大框架:Clouda.SiteApp.Appbuilder MBaaS解决高性能Server很难构建的问题. 百度开放云的区域运营服务于创业者 ------- ...
- 关于JavaScript的那些话
1.初学者动手环境----推荐Chrome的控制台(F12调用)2.JS中两个非常重要的数据类型是对象和数组.3.JavaScript 程序是用Unicode字符集编写的.4.JavaScript是区 ...
- vfd折腾(一)
从一开始驱动一块翻出来的液晶显示屏就想做一个电子时钟,偶然翻到了vfd(Vacuum Fluorescent Display的缩写,意为真空荧光显示屏). 此后就走上了不归路
- NSURLSession 所有的都在这里(一)
这篇文章会有什么? 在这篇文章中把NSURLSession.h文件集体梳理一遍,把里面的每个属性.代理和方法都拿出来说说,通过这篇文章我相信对于NSURLSession这一块的东西会梳理的比较全面一点 ...
- 微信小程序入门一
基本的准备工作 -知识储备 --基础:HTML+JS+CSS --进阶:React.Vue -工具安装 --工具由微信官方提供 ---下载地址:https://github.com/zce/weapp ...
- Navicat永久激活步骤,激活工具,解决注册码无效的问题
Navicat for MySQL是一套管理和开发MySQL或MariaDB的理想解决方案,支持单一程序,可同时连接到MySQL和MariaDB.这个功能齐备的前端软件为数据库管理.开发和维护提供了直 ...
- 关于JAVA中异常处理的简单阐释.
---恢复内容开始--- 这是我的一篇要在博客园发布的随笔,主要是简单的概括一下我本次所学的关于异常处理的知识.有讲的不妥当的地方,或者有需要补充的,还请各位高人给指点,共同学习,虚心求学.谢谢啦~ ...
- Linux(二十二)Ubuntu安装和配置
Ubuntu的介绍 Ubuntu是一个以桌面应用为主的开源GNU/Linux操作系统,Ubuntu是基于GNU/Linux,支持x86.amd64(即x64)和ppc架构,由全球化的专业开发团队(Ca ...
- Robot Framework之测试用例分层实战
1.1 测试用例的第一层(交互层) 1. 创建项目资源(Resource). 操作步骤: 点”项目名称”->右键,选New Resource,在弹窗Name 输入框输入资源名称 mykeywo ...
- Java动态代理(一)
好久没有动笔了,最近想巩固一下自己的基础知识,最近听到一同事问为什么JDK动态代理不能代理类,一听感觉懵逼呀!自己好像也不能很好的描述出来,所以想用2篇文章来复习一下动态代理知识: 一.什么是静态代理 ...