1.安装axios: cnpm install --save axios
2.在main.js中引入
import axios from '.axios'
Vue.prototype.ajax = axios
3.简单调用
①get请求
mounted () {
this.$ajax({
method: 'get',
url:'写入完整的接口地址'
}).then(res => {
alert(res.data.data)
}).catch(err =>{
alert('请求错误')
})
}
②post请求 传参时
var qs =request('qs')
var url = "接口地址"
this.$ajax.post(url, qs.stringify({
city: '张家口',
})).then(res => {
alert('请求成功')
}).catch(err => {
alert('请求失败')
})