vue全家桶项目学习(四、axis)

一、安装与使用

  1. 安装

npm install -S axios

  1. 使用
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

猜你喜欢

转载自blog.csdn.net/qq_42555578/article/details/107918442