vue-cli的项目中关于axios的全局配置,结合element UI,配置全局loading,header中做token传输
在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传输的更多相关文章
- vue cli搭建项目及文件引入
cli搭建方法:需安装nodejs先 1.npm install -g cnpm --registry=https://registry.npm.taobao.org //安装cnpm,用cnpm下载 ...
- 解决@vue/cli 创建项目是安装chromedriver时失败的问题
最近在使用新版vue的命令行工具创建项目时,安装chromedriver老是失败,导致后面的步骤也没有进行.网上搜索了一下,全是使用 工作中常见问题汇总及解决方案 npm install chrome ...
- Vue CLI 创建项目
使用命令创建VUE项目 运行以下命令[vue create [项目名]]来创建一个新项目: vue create hello-world 警告 如果你在 Windows 上通过 minTTY 使用 G ...
- vue cli 打包项目造成css背景图路径错误
vue cli做项目的时候难免会碰到,css引用背景图或者css,js之间的相互引用!!!这时候打包后可能会出现一个错误!!如下图: 写法: 错误: 会无端多出一个“/css/static/” 这样就 ...
- VUE学习笔记之vue cli 构建项目
一.环境搭建: 1.安装node.js 从node.js官网下载并安装node,安装过程很简单,一路"下一步"就可以了.安装完成之后,打开命令行工具(win+r,然后输入cmd), ...
- vue Cli 按需引入Element UI 和全局引用Element UI
全局引用: 一.安装 Element UI npm i element-ui -S 二.在main.js 中引入 element UI import ElementUI from 'element-u ...
- Vue -cli 入门 --项目搭建(一)
一. 安装node.js环境. 在node.js官网下载稳定版本(https://nodejs.org/en/) 下载完成后点击安装,安装过程很简单,一直next即可,安装完成会自动添加node及np ...
- vue中使用axios进行ajax请求数据(跨域配置)
npm安装axios npm install axios --save 引入axios import axios from 'axios' 使用axios mounted () { this.getH ...
- vue中使用axios给生产环境和开发环境配置不同的baseUrl
第一步:设置不同的接口地址 找到文件:/config/dev.env.js 代码修改为: var merge = require('webpack-merge') var prodEnv = requ ...
随机推荐
- 利用Struts上传文件
在利用struts2完成上传文件到服务器时,遇到获取不到文件名 原因是在Action中的属性名没有和jsp中的属性名匹配 <%@ page language="java" i ...
- [TJOI2019]唱、跳、rap和篮球——容斥原理+生成函数
先附一组sd图 然后放上原题链接 注意,队伍不同指的是喜好不同,不是人不同 先想到\(DP\),然后你会发现并没有什么优秀的状态设计,然后我们考虑容斥 设\(lim\)表示选的癌坤组数的上限,\(f_ ...
- Java中wait()与notify()理解
通常,多线程之间需要协调工作.例如,浏览器的一个显示图片的线程displayThread想要执行显示图片的任务,必须等待下载线程 downloadThread将该图片下载完毕.如果图片还没有下载完,d ...
- 012_linuxC++之_类的继承定义
(一)访问控制和继承 公有继承(public):当一个类派生自公有基类时,基类的公有成员也是派生类的公有成员,基类的保护成员也是派生类的保护成员,基类的私有成员不能直接被派生类访问,但是可以通过调用基 ...
- 5.13T1Send 题(send)
Send 题(send) [题目描述] 某个国家有
- 浅谈C++运算符重载
首先,什么是运算符重载? C++里运算符重载主要有两种方法.一是通过类的成员函数进行重载,二是通过类的友元函数进行重载. 下面以简单的复数类complex为例: 下面是通过类的成员函数进行运算符的重载 ...
- codeforces#1166F. Vicky's Delivery (Service并查集+启发式合并)
题目链接: https://codeforces.com/contest/1166/problem/F 题意: 给出节点数为$n$,边数为$m$的图,保证每个点对都是互连的 定义彩虹路:这条路经过$k ...
- jquery转换js
刚离职,一直忙于弄简历,整理面试题.今天得空吧前几天学习复习的jq基础知识整理一下,长时间不用还真的忘记了.所有在深入学习中也不要忘记复习之前的知识.做巩固,老话说的好打好根基才能盖好房.基础知识过后 ...
- lavarel数据库查找别名操作
lavarel数据库查找别名操作 一.总结 一句话总结: 当有表前缀的时候:DB::table('users as table1')->select(DB::raw('table1.id'))- ...
- phpstorm配置了git后Terminal 不能使用显示:git' 不是内部或外部命令,也不是可运行的程序
问题:在phpstorm上配置好git后,将代码拉了下来 ,但是命令行无法使用显示如图 解决方法:①找到安装git的位置,然后在该目录的子目录下分别找到git-core.bin 两个目录,我的安装在了 ...