一、安装与使用
- 安装
npm install -S axios
- 使用
import axios from 'axios';
export default {
mounted () {
axios.post('http://xxx.xxx.xxx.xxx/getInfo', { param: 'test' }).then(res => {
...
}).catch(err => {
...
})
}
}
二、跨域
vue.config.js
中设置代理
module.exports = {
devServer: {
// 服务器地址
proxy: 'http://xxx.xxx.xxx.xxx'
}
}
axios.post('/getInfo', { param: 'test' }).then(res => {
...
}).catch(err => {
...
})
懒得写了,还可自己封装(生产与开发环境baseUrl,header,请求拦截,响应拦截…)。可在单独写一个js文件进行封装,然后在main.js中引入,vue.prototype.$xxx = xxx
在原型中定义,然后直接在组件中this.$xxx()
使用。
参考:
https://www.kancloud.cn/yunye/axios/234845
http://www.axios-js.com/zh-cn/docs/vue-axios.html