详解Vue整合axios的实例代码
2017-06-21 15:20
1111 查看
在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); }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- iOS 二维码生成及扫码详解及实例代码
- IOS点击按钮隐藏状态栏详解及实例代码
- Vue组件中prop属性使用说明实例代码详解
- vue-cli学习——axios实例
- IOS 七种手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)详解及实例代码
- 加载 vue 远程代码的组件实例详解
- 详解vue中axios的封装
- vue.js整合mint-ui里的轮播图实例代码
- IOS 自定义UIPickView详解及实例代码
- 详解MongoDB和Spring整合的实例代码
- IOS Object-C 中Runtime详解及实例代码
- vue 下列表侧滑操作实例代码详解
- Spring mvc+struts2+Hibernate整合实例代码(注解详解)
- IOS开发 UIAlertController详解及实例代码
- Vue开发中整合axios的文件整理
- IOS代码修改音量实例详解
- vue-cli 使用axios的操作方法及整合axios的多种方法
- vue中Axios的封装与API接口的管理详解
- 二维码扫码登录详解【附简易实例代码(html+php+ios)】
- iOS多线程GCD之队列和任务详解(实例代码验证篇)