Vue开发中整合axios的文件整理


Posted in Javascript onApril 29, 2017

前言

大家在vue日常开发中,不可避免要整合axios,这里简单记录一下整合中的文件,方便以后使用查找。下面来看看详细的介绍。

整合方法

整合文件axios.js

import axios from 'axios';
// 适配vue-resource
const instance = axios.create();
instance.interceptors.request.use(config=> {
//Serialize.decode(config);
return config;
});
instance.interceptors.response.use(response=> {
return response.data;
}, err=> {
if (err.response) {
axios.post('/v1/error', err.response);
return Promise.reject(err.response.data);
}
return Promise.reject({ code: 1024, message: err.message });
});
function plugin(Vue) {
if (plugin.installed) {
return;
}
Vue.http = instance;
}
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(plugin);
}
export default plugin;

vue插件使用 app.js

import Vue from 'vue';
import App from './App.vue';
import store from './store';
import { sync } from 'vuex-router-sync';
import router from './router';
import * as filters from './filters';
import yxui from 'yxui/dist/yxui.min';
import axios from './axios';
Vue.use(yxui);
Vue.use(axios);
// sync the router with the vuex store.
// this registers `store.state.route`
sync(store, router);
// register global utility filters.
Object.keys(filters).forEach(key => {
Vue.filter(key, filters[key]);
});
// create the app instance.
// here we inject the router and store to all child components,
// making them available everywhere as `this.$router` and `this.$store`.
const app = new Vue({
router,
store,
...App
});
// expose the app, the router and the store.
// note we not mounting the app here, since bootstrapping will be
// different depending on whether we are in browser or on the server.
export { app, router, store };

在vuex action 中使用:

actions: {
// adList
[TypesAds.AD_GET_LIST](ctx, params){
return Vue.http.get('/v1/api/ads/list', {params}).then(data=> {
ctx.commit(TypesAds.AD_GET_LIST, data);
return data;
}).catch(err=> {
//统一错误处理
Vue.$message.error(err.msg);
});
}
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Javascript 相关文章推荐
Prototype使用指南之enumerable.js
Jan 10 Javascript
利用jQuary实现文字浮动提示效果示例代码
Dec 26 Javascript
JavaScript事件委托用法分析
Jan 24 Javascript
jquery模拟多级复选框效果的简单实例
Jun 08 Javascript
jquery插件canvaspercent.js实现百分比圆饼效果
Jul 18 jQuery
EasyUI创建人员树的实例代码
Sep 15 Javascript
Vue 父子组件的数据传递、修改和更新方法
Mar 01 Javascript
jQuery+ajax读取json数据并按照价格排序示例
Mar 28 jQuery
Vue框架里使用Swiper的方法示例
Sep 20 Javascript
vue 实现Web端的定位功能 获取经纬度
Aug 08 Javascript
Vue2.X和Vue3.0数据响应原理变化的区别
Nov 07 Javascript
记一次react前端项目打包优化的方法
Mar 30 Javascript
Vue.js教程之axios与网络传输的学习实践
Apr 29 #Javascript
jQuery输入框密码的显示隐藏【代码分享】
Apr 29 #jQuery
jQuery实现多张图片上传预览(不经过后端处理)
Apr 29 #jQuery
jQuery设置图片等比例缩小的方法
Apr 29 #jQuery
Angular2下使用pdf插件的方法详解
Apr 29 #Javascript
vue.js利用defineProperty实现数据的双向绑定
Apr 28 #Javascript
node.js+jQuery实现用户登录注册AJAX交互
Apr 28 #jQuery
You might like
PHP实现多服务器session共享之NFS共享的方法
2007/03/16 PHP
php基于PDO实现功能强大的MYSQL封装类实例
2017/02/27 PHP
addRule在firefox下的兼容写法
2006/11/30 Javascript
JavaScript数值转换的三种方式总结
2014/07/31 Javascript
JavaScript计时器示例分析
2015/02/05 Javascript
早该知道的7个JavaScript技巧
2016/06/21 Javascript
js 转json格式的字符串为对象或数组(前后台)的方法
2016/11/02 Javascript
jQuery接受后台传递的List的实例详解
2017/08/02 jQuery
vue-cli实现多页面多路由的示例代码
2018/01/30 Javascript
详解Angular5路由传值方式及其相关问题
2018/04/28 Javascript
微信小程序动态生成二维码的实现代码
2018/07/25 Javascript
vue 自定义提示框(Toast)组件的实现代码
2018/08/17 Javascript
vue基于element的区间选择组件
2018/09/07 Javascript
Javascript实现动态时钟效果
2018/11/17 Javascript
每周一练 之 数据结构与算法(Stack)
2019/04/16 Javascript
Vue 前端实现登陆拦截及axios 拦截器的使用
2019/07/17 Javascript
如何在vue中使用jointjs过程解析
2020/05/29 Javascript
Vue watch响应数据实现方法解析
2020/07/10 Javascript
解决vant-UI库修改样式无效的问题
2020/11/03 Javascript
Python标准异常和异常处理详解
2015/02/02 Python
python中偏函数partial用法实例分析
2015/07/08 Python
使用Python写一个小游戏
2018/04/02 Python
深入浅析python 协程与go协程的区别
2019/05/09 Python
浅谈Tensorflow 动态双向RNN的输出问题
2020/01/20 Python
浅谈pytorch 模型 .pt, .pth, .pkl的区别及模型保存方式
2020/05/25 Python
Python StringIO及BytesIO包使用方法解析
2020/06/15 Python
Python 删除List元素的三种方法remove、pop、del
2020/11/16 Python
浅谈基于Canvas的手绘风格图形库Rough.js
2018/03/19 HTML / CSS
你所知道的集合类都有哪些?主要方法?
2012/12/31 面试题
优秀研究生自我鉴定
2013/12/04 职场文书
高中自我鉴定
2013/12/20 职场文书
大学生应聘导游自荐信
2014/06/02 职场文书
暑期学习心得体会
2014/09/02 职场文书
初中生散播谣言检讨书
2014/11/17 职场文书
公司规章制度范本
2015/08/03 职场文书
和领导吃饭祝酒词
2015/08/11 职场文书