详解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 相关文章推荐
[原创]用javascript实现检测指定目录是否存在的方法
Jan 12 Javascript
javascript重复绑定事件造成的后果说明
Mar 02 Javascript
js实现广告漂浮效果的小例子
Jul 02 Javascript
CheckBoxList多选样式jquery、C#获取选择项
Sep 06 Javascript
JQuery中Text方法用法实例分析
May 18 Javascript
js微信分享API
Oct 11 Javascript
AngularJs 动态加载模块和依赖
Sep 15 Javascript
Angular ui.bootstrap.pagination分页
Jan 20 Javascript
深入理解vue Render函数
Jul 19 Javascript
原生js jquery ajax请求以及jsonp的调用方法
Aug 04 jQuery
浅谈Vue 数据响应式原理
May 07 Javascript
p5.js临摹旋转爱心
Oct 23 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
thinkPHP学习笔记之安装配置篇
2015/03/05 PHP
PHP之图片上传类实例代码(加了缩略图)
2016/06/30 PHP
thinkphp5使用无限极分类
2019/02/18 PHP
PHP时间类完整代码实例
2021/02/26 PHP
jquery 操作单选框,复选框,下拉列表实现代码
2009/10/27 Javascript
基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码(带平滑移动的效果)
2011/05/24 Javascript
jquery随意添加移除html的实现代码
2011/06/21 Javascript
JavaScript中的面向对象介绍
2012/06/30 Javascript
解决js数据包含加号+通过ajax传到后台时出现连接错误
2013/08/01 Javascript
jquery中的常用事件bind、hover、toggle等示例介绍
2014/07/21 Javascript
JavaScript中的this引用(推荐)
2016/08/05 Javascript
js实现键盘自动打字效果
2016/12/23 Javascript
js时间戳格式化成日期格式的多种方法介绍
2017/02/16 Javascript
使用vs code开发Nodejs程序的使用方法
2017/09/21 NodeJs
浅谈 vue 中的 watcher
2017/12/04 Javascript
微信小程序学习笔记之本地数据缓存功能详解
2019/03/29 Javascript
微信小程序实现吸顶特效
2020/01/08 Javascript
浅谈vue 二级路由嵌套和二级路由高亮问题
2020/08/06 Javascript
[01:15:18]2014 DOTA2国际邀请赛中国区预选赛 LGD VS Speed Gaming.cn
2014/05/22 DOTA
Python实现的下载网页源码功能示例
2017/06/13 Python
Python2.7环境Flask框架安装简明教程【已测试】
2018/07/13 Python
Django web框架使用url path name详解
2019/04/29 Python
python ubplot使用方法解析
2020/01/10 Python
ubuntu16.04升级Python3.5到Python3.7的方法步骤
2020/08/20 Python
将世界上最美丽的摄影作品转化为艺术作品:Photos.com
2017/11/28 全球购物
大型车展策划方案
2014/02/01 职场文书
新年抽奖获奖感言
2014/03/02 职场文书
授权委托书范本
2014/04/03 职场文书
甘南现象心得体会
2014/09/11 职场文书
初中生旷课检讨书范文
2014/10/06 职场文书
综治工作汇报材料
2014/10/27 职场文书
2015年乡镇妇联工作总结
2015/05/19 职场文书
赞助商致辞
2015/07/30 职场文书
机关单位2016年法制宣传日活动总结
2016/04/01 职场文书
各种货币符号快捷输入
2022/02/17 杂记
零基础学java之带返回值的方法的定义和调用
2022/04/10 Java/Android