1:axios的封装
import axios from 'axios'
import {Toast} from 'vant';
import store from '../store/store.js'
export const Axios = axios.create({
baseURL: 'https://ont-api.gotodns.win/',
baseURL: 'https://bc.sarl/',
timeout: 5000,
})
Axios.interceptors.request.use(function(config) {
if (store.state.token) {
config.headers.token = `${store.state.token}`;
}
return config;
}, function(error) {
return Promise.reject(error);
});
Axios.interceptors.response.use(function(response) {
if (response.status === 200) {
return Promise.resolve(response);
} else {
return Promise.reject(response);
}
return response;
}, function(error) {
return Promise.reject(error);
});
Axios.defaults.transformResponse = [function(data) {
try {
var res = $.parseJSON(data);
if (res.code == -1000) {
sessionStorage.clear();
window.location.href = "/"
}
return res;
} catch (e) {
}
}];
export default {
install(Vue) {
Object.defineProperty(Vue.prototype, '$http', {
value: Axios
})
}
}
2:在mian中引用
import Axios from './api/http'
Vue.use(Axios)
Vue.config.productionTip = false