一、配置axios
打开项目中的package.json
添加以下依赖:
"dependencies": {
"axios": "^0.19.0"
},
打开main.js
在其中加入下图红框中的代码:
即:
import axios from 'axios'
Vue.prototype.axios = axios;
二、使用axios
get方式,不带参数
this.axios.get('url')
.then(function (res) {
//成功
this.stus = res.data;//此为后台响应回来的数据
}.bind(this))
.catch(function (err) {
if (err.response) {
//失败
console.log(err.response)
}
}.bind(this))
get方式,带参数
this.axios.get('url', {
params: {
参数名1: 参数值1,
参数名2: 参数值2,
}
})
.then(function (res) {
//成功
}.bind(this))
.catch(function (err) {
if (err.response) {
console.log(err.response)
}
}.bind(this))
post方式
this.axios.post("url", js对象)
.then(function (res) {
//成功
}.bind(this))
.catch(function (err) {
if (err.response) {
console.log(err.response)
}
}.bind(this))