// create an axios instance const service = axios.create({ baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url withCredentials: true, // send cookies when cross-domain requests timeout: 500000, // request timeout transformRequest: [function(data) { // 对 data 进行任意转换处理 returnQs.stringify({ ...data }, // 数组的转换 { arrayFormat: 'brackets' }) }] })
// request interceptor service.interceptors.request.use( config => { // do something before request is sent
if (store.getters.token) { // let each request carry token // ['X-Token'] is a custom headers key // please modify it according to the actual situation config.headers['token'] = getToken() } return config }, error => { // do something with request error console.log(error) // for debug returnPromise.reject(error) } )
发起请求的方法:
1 2 3 4 5 6 7
exportfunctionremoteApi(data) { returnrequest({ url: '/api', method: 'post', data }) }