在src目录中建立plugins文件夹,在文件夹内建立axios.js文件

"use strict";

import Vue from 'vue';
import axios from "axios";
import {
getCookie,
delCookie,
showFullScreenLoading,
tryHideFullScreenLoading
} from '../utils/auth'
import {
Message,
} from 'element-ui' axios.defaults.headers.post['Content-Type'] = 'application/json'; let config = {
baseURL: 'http://jiekou.com/',
timeout: 60 * 1000, // Timeout
showLoading: true,//是否需要loading效果,如果不需要,则在请求时的第三个参数中传{showLoading:false}
// withCredentials: true, // Check cross-site Access-Control
}; const token = getCookie('token');
const _axios = axios.create(config); _axios.interceptors.request.use(
function (config) {
// Do something before request is sent
if (config.showLoading) {
showFullScreenLoading()
}
config.headers.common['Authorization'] = 'Bearer ' + token;
return config;
},
function (error) {
// Do something with request error
if (config.showLoading) {
tryHideFullScreenLoading();
}
return Promise.reject(error);
}
);
_axios.all = axios.all;
_axios.spread = axios.spread;
// Add a response interceptor
_axios.interceptors.response.use(
function (response) { if (config.showLoading) {
tryHideFullScreenLoading();
}
if (response.data.Type == 401) {
delCookie('token');
Message.error('登录信息失效,稍后将自动为您转至登录页,请重新登录!');
setTimeout(function () {
location.href = '/login';
}, 3000);
}else if(response.data.Type==500 || response.data.Type==203){
Message.error("警告:" + response.data.Content);
} return response;
},
function (error) {
if (config.showLoading) {
tryHideFullScreenLoading();
}
// Do something with response error
return Promise.reject(error);
}
); Plugin.install = function (Vue, options) {
Vue.axios = _axios;
window.axios = _axios;
Object.defineProperties(Vue.prototype, {
axios: {
get() {
return _axios;
}
},
$axios: {
get() {
return _axios;
}
},
});
}; Vue.use(Plugin) export default Plugin;

  

  在axios文件中,我们引入了cookie操作和loading加载的方法。那么我们再来看看引入文件是什么。首先在src文件夹下创建utils文件夹,文件夹内创建auth.js。auth.js内是方法

import { Loading } from 'element-ui'
import _ from 'lodash' export function getCookie(objName) {
var arrStr = document.cookie.split("; ");
for (var i = 0; i < arrStr.length; i++) {
var temp = arrStr[i].split("=");
if (temp[0] == objName) return unescape(temp[1]);
}
} export function delCookie(name){
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toString();
} /**
* 全局loading的封装
* **/ let loading;
let needLoadingRequestCount = 0; function startLoading() {
loading = Loading.service({
lock: true,
text: '加载中……',
background: 'rgba(0, 0, 0, 0.7)'
})
} const tryCloseLoading = () => {
if (needLoadingRequestCount === 0) {
loading.close()
}
} export function showFullScreenLoading() {
if (needLoadingRequestCount === 0) {
startLoading()
}
needLoadingRequestCount++
} export function tryHideFullScreenLoading() {
if (needLoadingRequestCount <= 0) return
needLoadingRequestCount--
if (needLoadingRequestCount === 0) {
_.debounce(tryCloseLoading, 300)();
}
} /**
* 全局loading的封装
* **/

  最后在main.js引入即可

import './plugins/axios'

返回目录

vue-cli的项目中关于axios的全局配置,结合element UI,配置全局loading,header中做token传输的更多相关文章

  1. vue cli搭建项目及文件引入

    cli搭建方法:需安装nodejs先 1.npm install -g cnpm --registry=https://registry.npm.taobao.org //安装cnpm,用cnpm下载 ...

  2. 解决@vue/cli 创建项目是安装chromedriver时失败的问题

    最近在使用新版vue的命令行工具创建项目时,安装chromedriver老是失败,导致后面的步骤也没有进行.网上搜索了一下,全是使用 工作中常见问题汇总及解决方案 npm install chrome ...

  3. Vue CLI 创建项目

    使用命令创建VUE项目 运行以下命令[vue create [项目名]]来创建一个新项目: vue create hello-world 警告 如果你在 Windows 上通过 minTTY 使用 G ...

  4. vue cli 打包项目造成css背景图路径错误

    vue cli做项目的时候难免会碰到,css引用背景图或者css,js之间的相互引用!!!这时候打包后可能会出现一个错误!!如下图: 写法: 错误: 会无端多出一个“/css/static/” 这样就 ...

  5. VUE学习笔记之vue cli 构建项目

    一.环境搭建: 1.安装node.js 从node.js官网下载并安装node,安装过程很简单,一路"下一步"就可以了.安装完成之后,打开命令行工具(win+r,然后输入cmd), ...

  6. vue Cli 按需引入Element UI 和全局引用Element UI

    全局引用: 一.安装 Element UI npm i element-ui -S 二.在main.js 中引入 element UI import ElementUI from 'element-u ...

  7. Vue -cli 入门 --项目搭建(一)

    一. 安装node.js环境. 在node.js官网下载稳定版本(https://nodejs.org/en/) 下载完成后点击安装,安装过程很简单,一直next即可,安装完成会自动添加node及np ...

  8. vue中使用axios进行ajax请求数据(跨域配置)

    npm安装axios npm install axios --save 引入axios import axios from 'axios' 使用axios mounted () { this.getH ...

  9. vue中使用axios给生产环境和开发环境配置不同的baseUrl

    第一步:设置不同的接口地址 找到文件:/config/dev.env.js 代码修改为: var merge = require('webpack-merge') var prodEnv = requ ...

随机推荐

  1. 2-删除IPC$的方式

    一.使用命令临时删除IPC$的方式 1.查看IPC$是否启用 命令:net share 2.删除IPC$功能 命令:net share ipc$ /delete 注:使用命令删除后,重启服务器后,IP ...

  2. js 获取json对象的 键 和 值

    直接上图 结果:

  3. 2018-2019 ICPC, NEERC J. Streets and Avenues in Berhattan(DP)

    题目链接:https://codeforc.es/contest/1070/problem/J 题意:给出一个长度为 k 的字符串,选出 n 个和 m 个不同位置的字符构成两个字符串,使得两个字符串相 ...

  4. 洛谷P1140 相似基因【线性dp】

    题目:https://www.luogu.org/problemnew/show/P1140 题意: 给定两串基因串(只包含ATCG),在其中插入任意个‘-’使得他们匹配.(所以一共是5种字符) 这5 ...

  5. CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)

    起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...

  6. CSS测试题Ⅱ

    1.如何使用 CSS3 强制换行? A. word-wrap: break-word; B. text-wrap: break-word; C. text-wrap: force; D. text-w ...

  7. 根据xml文件生成javaBean

    原 根据xml文件生成javaBean 2017年08月15日 18:32:26 吃完喝完嚼益达 阅读数 1727 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出 ...

  8. Educational Codeforces Round 33 (Rated for Div. 2) B题

    B. Beautiful Divisors Recently Luba learned about a special kind of numbers that she calls beautiful ...

  9. http range request

    range request: 要求实现该功能需要指定下载的实体范围

  10. Win7下使用Visual Studio为WinXP编译可执行文件

    造冰箱的大熊猫@cnblogs 2019/8/5 2019/9/5补充:参见这里 1.问题 开发机是64位Win7,想开发能够运行在WinXP下的C程序,怎么办? 2.解决方法 一个简单粗暴的解决办法 ...