详解Vue整合axios的实例代码

 更新时间:2017年06月21日 15:20:06   作者:彬仔  
本篇文章主要介绍了详解Vue整合axios的实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解Vue路由History mode模式中页面无法渲染的原因及解决

    详解Vue路由History mode模式中页面无法渲染的原因及解决

    这篇文章主要介绍了详解Vue路由History mode模式中页面无法渲染的原因及解决,非常具有实用价值,需要的朋友可以参考下
    2017-09-09
  • 关于vue设置环境变量全流程

    关于vue设置环境变量全流程

    这篇文章主要介绍了关于vue设置环境变量全流程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 解析Vue.use()是干什么的

    解析Vue.use()是干什么的

    今天通过本文给大家分享Vue.use是什么,主要包括vueEsign 插件的install是什么,element-ui的install是什么,为什么有的库就不需要使用Vue.use,对vue.use()相关知识感兴趣的朋友一起看看吧
    2022-06-06
  • Vue实现push数组并删除的例子

    Vue实现push数组并删除的例子

    今天小编就为大家分享一篇Vue实现push数组并删除的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • 解决vue项目运行出现warnings potentially fixable with the `--fix` option的报错问题

    解决vue项目运行出现warnings potentially fixable with the `--fix

    这篇文章主要介绍了解决vue项目运行出现warnings potentially fixable with the `--fix` option的报错问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-11-11
  • 浅析vue插槽和作用域插槽的理解

    浅析vue插槽和作用域插槽的理解

    插槽,也就是slot,是组件的一块HTML模板,这块模板显示不现实、以及怎样显示由父组件来决定。这篇文章主要介绍了浅析vue插槽和作用域插槽的理解,需要的朋友可以参考下
    2019-04-04
  • Vue使用vm.$set()解决对象新增属性不能响应的问题

    Vue使用vm.$set()解决对象新增属性不能响应的问题

    这篇文章主要介绍了Vue使用vm.$set()解决对象新增属性不能响应的问题,为了解决这个问题,Vue提供了一个特殊的方法vm.$set(object, propertyName, value),也可以使用全局的Vue.set(object, propertyName, value)方法,需要的朋友可以参考下
    2023-05-05
  • 详解vue-cli与webpack结合如何处理静态资源

    详解vue-cli与webpack结合如何处理静态资源

    本篇文章主要介绍了详解vue-cli与webpack结合如何处理静态资源,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • ElementUI如何修改Dialog的标题样式

    ElementUI如何修改Dialog的标题样式

    这篇文章主要介绍了ElementUI如何修改Dialog的标题样式问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • vue项目中监听手机物理返回键的实现

    vue项目中监听手机物理返回键的实现

    这篇文章主要介绍了vue项目中监听手机物理返回键的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01

最新评论