vue axios请求方式和传参格式

vue axios请求方式和传参格式

在这里插入图片描述
get、delete、head请求只有两个参数,请求参数是放在第二个参数config中,post、put、patch请求有3个参数,请求参数是放在第二个参数data中的。

  1. 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'
	}
})
  1. post、put、patch的请求,请求参数是放在请求体中的(实际项目只是用到了post,其他2种没有用过)
  • post请求, 参数放在第二个参数data中
axios.post('url地址', {
		id: 12121,
		name: 'huahua'
})
  1. 同时发生的请求 使用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')
	},
}
发布了41 篇原创文章 · 获赞 3 · 访问量 6397

猜你喜欢

转载自blog.csdn.net/weixin_40509884/article/details/100076582