在請求封裝頁面xx.js中
import axios from 'axios'
// axios 基本配置
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: 5000 // 請求超時(shí)時(shí)間
})
// 添加請求攔截器
service.interceptors.request.use(function (config) {
// 在發(fā)送請求之前做些什么
return config;
}, function (error) {
// 對請求錯(cuò)誤做些什么
return Promise.reject(error);
});
// 添加響應(yīng)攔截器
service.interceptors.response.use(function (response) {
// 對響應(yīng)數(shù)據(jù)做點(diǎn)什么
return response;
}, function (error) {
// 對響應(yīng)錯(cuò)誤做點(diǎn)什么
return Promise.reject(error);
});
export function axiosPost(url,data={}) {
return new Promise((resolve, reject) => {
service.post(url,data).then(res=>{
resolve(res.data)
}),err=>{
reject(err)
}
})
}
在統(tǒng)一的api文件中
import { axiosPost } from "../http/request";
export const indexInfo = (data={})=>axiosPost('url',data);
使用頁面中
async getInfo(){
const Info = await indexInfo()
this.AboutInfo = Info
console.log(Info)
}