Vue 整合 axios 发表于 2017-02-04 | 分类于 前端 , Vue | | 阅读次数 在vue开发中,不可避免要整合axios,简单记录一下整合中的文件,方便以后使用查找。 整合文件axios.js1234567891011121314151617181920212223242526272829303132import axios from 'axios';// 适配vue-resourceconst 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.js123456789101112131415161718192021222324252627282930313233343536import 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 中使用:123456789101112actions: {// 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);});}} 测试功能而已,你非要赏点我就没办法了... 赏 微信打赏 支付宝打赏