axios和vue-axios入门使用

npm安装:npm install axios vue-axios --save
main.js引入axios和vue-axios
import axios from “axios”
import Vueaxios from “vue-axios”
Vue.use(Vueaxios,axios)
注释:axios是一个库,不是vue的第三方插件,不能通过Vue.use()方法安装插件,需要在原型进行绑定Vue.prototype.$http = axios;vue-axios是vue的三方插件,通过全局方法Vue.use()使用插件;
axios的方法get、post、put、patch、delete

get,delete的两个参数url,config

axios原型添加axios:
	axios.get().then();
axios和vue-axios一起使用:
	this.axios.get().then()
axios.get('/data.json',{
		params:{
			id: 1
		}
	}).then(res=>{
		console.log(res)
	})
	//http://localhost:8080/data.json?id=1axios({
		method: 'get',
		url:'/data.json'
		params:{
			id:1
		}
	})
	//post 两种上传方式applicition/json和form-data
	let data = {
		id:1
	}
	axios.post('/data.json',data).then(res=>{
		console.log(res)
	})
	或者
	axios({
		method:'post',
		url:'/data.json',
		data:data
	}).then(res=>{
		console.log(res)
	})
	//post form-data
	let formData = new FormData()
	for(let key in data){
		formData.append(key,data[key])
	}
	axios.post('post',formData).then(res=>{
		console.log(res)
	})
	//put 
	axios.put('put',data).then(res=>{
		console.log(res)
	})
	//patch
	axios.patch('patch',data).then(res=>{
		console.log(res)
	})
	//delete
	axios.delete('delete',{
		params:{
			id:1
		}
	}).then(res=>{
		console.log(res)
	})
	或者
	axios.delete('delete',{
		data:{
			id:1
		}
	}).then(res=>{
		console.log(res)
	})

并发请求:同时处理多个接口并同一处理数据

	axios.all([
		axios.get('./data1.json')
		axios.get("./data2.json")
	]).then(
		axios.spread((data1res,data2res)=>{
			console.log(data1res,data2res)
		})
	)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
axios是一个基于Promise的HTTP客户端库,可以用于浏览器和Node.js环境中向服务器发送HTTP请求。它是一个功能强大且易于使用的库,可以与Vue.js等前端框架很好地配合使用vue-axios是一个适用于Vue.js的插件,它将axios集成到Vue.js中,使我们可以在Vue组件中更方便地使用axios发送HTTP请求。通过使用vue-axios插件,我们可以在Vue实例和Vue组件中直接使用this.$http来代替axios实例来发送请求,简化了我们的代码并提高了开发效率。 虽然我们也可以直接使用axios来发送请求,但使用vue-axios插件更符合Vue整体生态环境的设计原则。直接写原型链的方式可能会显得比较粗暴,并且不太推荐,除非是在底层实现的时候。因此,我们更推荐使用vue-axios插件的方式来结合Vue.js框架使用axios。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [axiosvue-axios](https://blog.csdn.net/qq_43654065/article/details/121288529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Vue axiosvue-axios的关系及使用区别](https://blog.csdn.net/u014641168/article/details/126096526)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长影缚苍龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值