当前位置: 移动技术网 > IT编程>脚本编程>vue.js > Vue开发中整合axios的文件整理

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

2018年04月30日  | 移动技术网IT编程  | 我要评论

汽油一吨等于多少升,新醉打金枝演员表,翟洪森

前言

大家在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);
});
}
}

总结

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网