# 利用npm install axios –save
## 在 utils中新建一个axios.js
[code]import axios from \"axios\";import api from \"../config/api.js\";import msg from \"../utils/msg.js\";import qs from \"qs\";// axios.defaults.withCredentials = true;const $axios = axios.create({// baseURL: process.env.NODE_ENV === \'production\' ? \'\' : config.baseURL,baseURL: api.host,timeout: 60000,headers: {post: { \"Content-Type\": \"application/x-www-form-urlencoded; charset=utf-8\" }}});$axios.interceptors.request.use(config => {if (config.method == \"post\") {console.log(qs.stringify(config.data));config.data = qs.stringify(config.data);}return config;},error => {return Promise.reject(error);});$axios.interceptors.response.use(res => {if (res.data.state == 0) {// 报错msg.fail(res.data.msg);} else {return res;}},err => {return Promise.reject(err);});export default $axios;
### 注意:如果后台做了限制post请求数据无法正常显示的话需要借助第三方插件qs
#### 在全局main.js中引用 import $axios from \”./utils/axios\”;在进行实例化:Vue.prototype.$http = $axios;