额
加油~
main.js文件
import axios from "./http";
Vue.prototype.$axios = axios;
http.js 文件
import axios from "axios"; import Qs from "qs"; if (process.env.NODE_ENV === "development") { // 设置默认本地axios提交url axios.defaults.baseURL = "http/https"; } else { // 设置默认线上axios提交url axios.defaults.baseURL = "http/https"; } axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; // 请求拦截 axios.interceptors.request.use( config => { if (config.method == "post") { // eslint-disable-next-line no-self-assign config.data = config.data; //防止post请求参数无法传到后台 } config.data = { ...config.data, access_token: token (有需要统一头部添加token) }; config.data = Qs.stringify(config.data); return config; }, error => { return Promise.reject(error); } ); // 响应拦截 axios.interceptors.response.use( function(res) { // 如果账号在其他地方登陆 if (res.data.errcode === "2") { // 提示错误 Toast({ message: "异地登录", duration: 1500 }); setTimeout(function() { sessionStorage.clear(); router.replace({ path: "/login" }); }, 1500); return false; } return res; }, function(error) { return Promise.reject(error); } ); export default axios;