1、先安装axios(以下安装都为npm安装)
npm install axios
在main.js里把axios重新赋值给Vue,如下:
import axios from 'axios';
Vue.prototype.$axios = axios;
2、安装qs
npm install qs
同样的把qs重新赋值给Vue,如下:
import Qs from 'qs';
Vue.prototype.$qs = Qs;
这样,axios和qs就可以全局使用了,下面是我的一个demo:
3、post请求
var postdata = this.$qs.stringify({
name:'lucy',
password:'123456'
});
this.$axios.post(this.$baseAPI+'test.php',postdata).then(function (response) {
console.log(response)
}).catch(function (error) {
console.log(error);
});
4、get请求
var postdata = 'name=lucy&password=123456';
this.$axios.get(this.$baseAPI+'test.php?'+postdata).then(function (response) {
console.log(response)
}).catch(function (error) {
console.log(error);
});
希望能帮到初学vue.2x的小伙伴~