vue axios请求方式和传参格式
get、delete、head请求只有两个参数,请求参数是放在第二个参数config中,post、put、patch请求有3个参数,请求参数是放在第二个参数data中的。
- get和delete请求,请求参数格式是query格式,参数是在url后通过“&”拼接
- get请求,参数放在config中的params中
//接口全写请求: url地址?id=12121&name='huahua'
//或者直接拼接在接口中写axios.get("url地址?id=12121&name='huahua'")
axios.get('url地址', {
params: {
id: 12121,
name: 'huahua'
}
})
- delete请求, 参数放在config中的data中
//接口全写请求: url地址?id=12121&name='huahua'
//或者直接拼接在接口中写axios.delete("url地址?id=12121&name='huahua'")
axios.delete('url地址', {
data: {
id: 12121,
name: 'huahua'
}
})
- post、put、patch的请求,请求参数是放在请求体中的(实际项目只是用到了post,其他2种没有用过)
- post请求, 参数放在第二个参数data中
axios.post('url地址', {
id: 12121,
name: 'huahua'
})
- 同时发生的请求 使用axios.all和axios.spread
//axios.all([接口数组]).then(axios.spread( (接口返回数据) => {}))
mounted() {
axios.all([this.getListOne(), this.getListTwo()]).then(axios.spread((dataOne, dataTwo) => {
console.log(dataOne) //dataOne是接口getListOne返回的数据
console.log(dataTwo) //dataTwo是接口getListTwo返回的数据
}))
},
methods: {
getOneList() {
return axios.get('/getListOne')
},
getTwoList() {
return axios.get('/getListTwo')
},
}