1、创建请求request.js文件
1、引入所需要的东西
import axios from 'axios';
import qs from 'qs';
import global_config from '@/components/common/global_config';
2、创建请求对象
const request = axios.create({
baseURL: global_config.ipAdd,
timeout: 10000,
});
3、设置请求拦截器
request.interceptors.request.use(config => {
if (config.method === 'get') {
return config;
}
if (config.method === 'post' && config.qsFlag) {
config.data = qs.stringify(config.data);
}
return config;
}, err => {
return Promise.reject(err);
});
4、响应拦截器
request.interceptors.response.use(
response => {
return response;
},
error => {
return Promise.reject(error.response.data)
});
5、导出
export default request;
2、创建请求js
import request from '../utils/http';
export const PartnersList = query => {
return request({
url: '/dict/list/partners',
method: 'get',
params: query
})
}
export const ProjectApprove = params => {
return request({
url: '/project/approve',
method: 'post',
data: params,
qsFlag: false
})
}
3、在vue文件中使用
import { PartnersList, ProjectApprove } from '@/api/project'
PartnersList().then(res => {
this.PartnersList = res.data
})