当前位置: 首页 > 编程笔记 >

详解Vue整合axios的实例代码

云德辉
2023-03-14
本文向大家介绍详解Vue整合axios的实例代码,包括了详解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 axios请求拦截实例代码,包括了vue axios请求拦截实例代码的使用技巧和注意事项,需要的朋友参考一下 axios 简介 axios 是一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端,它本身具有以下特征: 从浏览器中创建 XMLHttpRequest 从 node.js 发出 http 请求 支持 Promise API 拦截请求和响应 转换请

  • 本文向大家介绍vue+axios实现登录拦截的实例代码,包括了vue+axios实现登录拦截的实例代码的使用技巧和注意事项,需要的朋友参考一下 一个项目学会vue全家桶+axios实现登录、拦截、登出功能,以及利用axios的http拦截器拦截请求和响应。 前言 该项目是利用了Github 提供的personal token作为登录token,通过token访问你的Repository List。

  • 本文向大家介绍SpringBoot整合Shiro的代码详解,包括了SpringBoot整合Shiro的代码详解的使用技巧和注意事项,需要的朋友参考一下 shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/  它提供了很方便的权限认证和登录的功能.   而springboot作为一个开源框架,必然提供了和shiro整合的功能!接下来就用springbo

  • 本文向大家介绍vue中axios请求的封装实例代码,包括了vue中axios请求的封装实例代码的使用技巧和注意事项,需要的朋友参考一下 axios Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中, 也是vue官方推荐使用的http库;封装axios,一方面为了以后维护方便,另一方面也可以对请求进行自定义处理 安装 封装 我把axios请求封装在htt

  • 本文向大家介绍Nginx的完整配置详解及实例代码,包括了Nginx的完整配置详解及实例代码的使用技巧和注意事项,需要的朋友参考一下 Nginx的完整配置详解            最近公司新项目,由于自己是小白,跟着项目经理对Nginx进行配置,在网上找到一篇相关资料,觉得不错,并可以完成配置,这里记录下, 实例代码: 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 本文向大家介绍Springboot整合Shiro的代码实例,包括了Springboot整合Shiro的代码实例的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了Springboot整合Shiro的代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1、导入依赖 2、创建ShiroRealm.java文件 (这里按照需求,只做登录认证

  • 本文向大家介绍springboot整合 beatlsql的实例代码,包括了springboot整合 beatlsql的实例代码的使用技巧和注意事项,需要的朋友参考一下 BeetSql是一个全功能DAO工具, 同时具有hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用。 beatlsql 优点 开发效率 无需注解,自动使用

  • 本文向大家介绍springboot与mybatis整合实例详解(完美融合),包括了springboot与mybatis整合实例详解(完美融合)的使用技巧和注意事项,需要的朋友参考一下 简介 从 Spring Boot 项目名称中的 Boot 可以看出来,Spring Boot 的作用在于创建和启动新的基于 Spring 框架的项目。它的目的是帮助开发人员很容易的创建出独立运行和产品级别的基于 Sp