axios二次封装
程序员文章站
2022-03-31 07:59:30
axios 二次封装//单独封装一个jsimport axios from "axios";import store from "../store/index"axios.defaults.baseURL="https://www.liulongbin.top:8888/api/private/v1";// 请求头axios.interceptors.request.use(config =>{// store.state.loading=true config.hea...
axios 二次封装
//单独封装一个js
import axios from "axios";
import store from "../store/index"
axios.defaults.baseURL="https://www.liulongbin.top:8888/api/private/v1";
// 请求头
axios.interceptors.request.use(config =>{
// store.state.loading=true
config.headers.Authorization =sessionStorage.getItem("token");
return config
})
// 响应
axios.interceptors.response.use(config =>{
store.commit('loading', false)
return config
})
let Request=(params)=>{
return new Promise((resolve, reject)=>{
axios({
...params,
}).then(res=>{
resolve(res);
}).catch(err=>{
reject(err);
})
})
}
export {Request}
在调用接口的时候引入
import {Request} from '../util/request'
export const login = (data)=>Request({
url: '/login',
method:'post',
params: data
})
export const menus=()=>Request({
url:'/menus',
method:'get',
// params,
})
// 用户列表获取
export const users=(data)=>Request({
url:'/user',
method:'get',
params: data
})
本文地址:https://blog.csdn.net/weixin_47249416/article/details/109671784