1.创建vue脚手架
vue init webpack demo
2.cd 项目根目录,再安装axios
npm install axios -S
3.在main.js中
//把axios赋值到vue的原型上,方便调用
Vue.prototype.$http = axios
4.在调用处
methods:{
axiosGet(){
let that = this;
that.$http.post('http://xxx.168.xx.220:5678/api/user/login',{
"UserAccount": "string",
"UserPassword": "string"
}).then(function(response){
console.log(response);
}).catch(function(error){
console.log(error);
})
}
}
5.可以在main.js中做一些配置
import qs from 'qs'
Vue.prototype.$http = axios
.create({
baseURL:'http://192.xx.10.xx:5678/api',
//请求前处理数据
transformRequest:[function(data){
console.log(data);
data=qs.stringify(data);
return data;
}],
//请求等待超时时间则中断
timeout: 1500,
//请求后的data处理
transformResponse: [function (data) {
console.log(data);
return data;
}]
})
6.配置后可以在调用处省略一些代码
that.$http({
method: 'post',
//这里的路径是和main.js中的baseURL拼接而来的
url: '/user/login',
data: {
"UserAccount": "string",
"UserPassword": "string"
}
})
.then(function(response){
console.log(response);
}).catch(function(error){
console.log(error);
})