详解Vue整合axios的实例代码


Posted in Javascript onJune 21, 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 });
});


functionplugin(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 相关文章推荐
js 利用className得到对象的实现代码
Nov 15 Javascript
js修改原型的属性使用介绍
Jan 26 Javascript
jQuery通过扩展实现抖动效果的方法
Mar 11 Javascript
浅谈JavaScript for循环 闭包
Jun 22 Javascript
jquery插件bootstrapValidator表单验证详解
Dec 15 Javascript
js实现年月日表单三级联动
Apr 17 Javascript
微信小程序网络请求wx.request详解及实例
May 18 Javascript
js es6系列教程 - 新的类语法实战选项卡(详解)
Sep 02 Javascript
微信小程序 POST请求的实例详解
Sep 29 Javascript
Three.js加载外部模型的教程详解
Nov 10 Javascript
vue动态合并单元格并添加小计合计功能示例
Nov 26 Vue.js
three.js 实现露珠滴落动画效果的示例代码
Mar 01 Javascript
详解在vue-cli项目中安装node-sass
Jun 21 #Javascript
详解vue-cli中配置sass
Jun 21 #Javascript
详解如何在vue中使用sass
Jun 21 #Javascript
JS表单提交验证、input(type=number) 去三角 刷新验证码
Jun 21 #Javascript
JS图片预加载插件详解
Jun 21 #Javascript
解决Extjs下拉框不显示的问题
Jun 21 #Javascript
AngularJS解决ng-if中的ng-model值无效的问题
Jun 21 #Javascript
You might like
php实现计算百度地图坐标之间距离的方法
2016/05/05 PHP
PHP利用超级全局变量$_GET来接收表单数据的实例
2016/11/05 PHP
thinkPHP中_initialize方法实例分析
2016/12/05 PHP
php设计模式之模板模式实例分析【星际争霸游戏案例】
2020/03/24 PHP
由prototype_1.3.1进入javascript殿堂-类的初探
2006/11/06 Javascript
js查找父节点的简单方法
2008/06/28 Javascript
JavaScript 函数调用规则
2009/09/14 Javascript
nodejs 后缀名判断限制代码
2011/03/31 NodeJs
JS加jquery简单实现标签元素的显示或隐藏
2013/09/23 Javascript
关于JavaScript作用域你想知道的一切
2016/02/04 Javascript
几种二级联动案例(jQuery\Array\Ajax php)
2016/08/13 Javascript
javascript基础知识讲解
2017/01/11 Javascript
原生js实现旋转木马轮播图效果
2017/02/27 Javascript
简述Angular 5 快速入门
2017/11/04 Javascript
vue iview实现动态路由和权限验证功能
2018/04/17 Javascript
详解swiper在vue中的应用(以3.0为例)
2018/09/20 Javascript
Koa代理Http请求的示例代码
2018/10/10 Javascript
jQuery实现简单的Ajax调用功能示例
2019/02/15 jQuery
Python yield使用方法示例
2013/12/04 Python
零基础写python爬虫之抓取百度贴吧代码分享
2014/11/06 Python
对于Python中线程问题的简单讲解
2015/04/03 Python
python开发之thread实现布朗运动的方法
2015/11/11 Python
python爬虫中get和post方法介绍以及cookie作用
2018/02/08 Python
对python pandas读取剪贴板内容的方法详解
2019/01/24 Python
PyQt 图解Qt Designer工具的使用方法
2019/08/06 Python
Python实现打印实心和空心菱形
2019/11/23 Python
python退出循环的方法
2020/06/18 Python
学python最电脑配置有要求么
2020/07/05 Python
如何使用python自带IDLE的几种方法
2020/10/10 Python
如何利用Python 进行边缘检测
2020/10/14 Python
浅谈基于HTML5的在线视频播放方案
2016/02/18 HTML / CSS
大学生期末自我鉴定
2014/02/01 职场文书
教师开学感言
2014/02/14 职场文书
机动车登记业务委托书
2014/10/08 职场文书
迟到检讨书
2015/01/26 职场文书
2016年社区创先争优活动总结
2016/04/05 职场文书