- axios.js
axios封装代码
- index.js
统一接口代码
- cs.vue
具体使用
axios.js
import Axios from 'axios';
const URL = process.env.NODE_ENV === 'production' ? 'http://' : 'http://';
Axios.defaults.baseURL = URL;
Axios.interceptors.request.use(
(config) => {
var token = 'xxxxx';
if (token) {
config['headers']['token'] = token;
} else {
}
return config;
},
error => Promise.reject(error),
);
Axios.interceptors.response.use(
(response) => {
return response.data;
},
error => Promise.reject(error.response),
);
export function axios(api, params, type, contentType = 'application/x-www-form-urlencoded') {
return new Promise((resolve, reject) => {
Axios({
url: api,
method: type ? type : 'post',
data: params,
headers: {
'Content-Type': contentType,
},
}).then((data) => {
resolve(data);
}).catch((err) => {
reject(err);
});
});
}
index.js
import { axios } from './axios';
export const login_api = params => post('/login', params);
cs.vue
import { login_api } from "../../application/api/index.js";
login_api(params).then(res => {
}).catch((err) => { });